Multiple TYPES of Data in a SINGLE VARIABLE in C++?

preview_player
Показать описание


Рекомендации по теме
Комментарии
Автор

std::variant *does* reuse space, and doesn't have a member for each data type. However it adds overhead which allows for run-time type checking. So there is a hidden int on the structure of std::variant which is identifying which value it's storing, which is why it's larger than a standard union. However if you use std::variant on many large types, it would only be slightly larger then the largest. After accounting for the type identifier and byte alignment.


So analogy of a type-safe union is 100% accurate in my opinion.

shadowmil
Автор

Teacher, the person in the back is playing on their computer instead of taking notes!

AmeshaSpentaArmaiti
Автор

Richard Stallman is sitting in the background! Wow!

tomershechner
Автор

from my experience std::variant does use a union but also has another member int to keep track of the in use type, so in your experiment : sizeof(data) = sizeof(union { std::string; int; }) + sizeof(int) and not sizeof(data) = sizeof(std::string) + sizeof(int). Both just happened to be 32 bytes

martian
Автор

Jesus! I didn't know there was a person in the background, and then it scared the shit out of me. :D

jack_oo
Автор

I thank you for teaching me to think about C++ in C++, not in C.
Too many people just write whatever they learned from C in C++, and say, "It works, though!".
But after watching your series, I found that to get the best out of this language and tool, it's best to think differently and forget about C paradigms sometimes.

SaifUlIslam-dbnu
Автор

The size difference between variant and union is more because it tracks the current active type so uses an extra 4 bytes, its a type safe union for that reason it knows the type.

reductor_
Автор

If for some reason you do not want to use std::visit, the preferred way to check what kind of data is inside would be std::holds_alternative<T>.

shushens
Автор

You can also use the std::holds_alternative<T> function for determining the type currently stored in the variant

warrenbuckley
Автор

Topics expected to be covered
a) SFINAE (std::enable_if)
b) Move semantics
c) Perfect forwarding

praveen
Автор

7:02 No, std::variant is not storing every data type as member, it really is a union of all types, plus an index.

Uluberlu
Автор

correct me if I'm wrong, but wouldn't data = false at 2:40 be functional code, since 'false' just evaluates to the integer 0?

AnonymouslyHidden
Автор

nice.. and for the error use case near the end you can now use std::expected in c++23.

oschonrock
Автор

Good thing you moved with you dope sofa. It radiates coziness and luxuriousness

denarz
Автор

Oh my god.
I just heard about this.
So hyped!!!

Subbed!!!

anonanon
Автор

Look who's sitting in the background!

laurensscheldeman
Автор

I'm a firsttime viewer, thanks for the video, it was very informative! Just wondering, who was that in the background, is that person ever in your videos with you?

SamCarleton
Автор

Isn't it the largest alignment or size of the given types + the size of an index(e.g. an int) that determines the size of the variant instead of the sum of the all the sizes of the types?

ariepiet
Автор

No it's not the sum of all the types, you should test it more thoroughly...
Variants are as efficient as tagged unions in size.

Since variants are type safe, they must save the type index of the object it holds (how can you query the index without it saving somewhere though), and that's a size_t which is 4 bytes under x86.
Thus the 32 you're seeing is not std::string(28)+int(4), it is std::string(28)+size_t(4, the index).
For example, under MSVC implementation and x86, std::string is 28 bytes and std::vector<int> is 16 bytes, and std::variant<std::string, std::vector<int>> is 32 bytes (max(28, 16) + 4).

ChlorieHCl
Автор

Thanks for this video, can you make a series on design patterns in c++, thanks in advance.

vivekshivhare