|
|
|
|
|
by chlorion
1368 days ago
|
|
Variants are meant to introduce a tagged union like data structure into C++. You can do this without variants by manually defining your own unions and managing the tag yourself, but this is very not type safe and requires extra boilerplate code to manage! Maybe you have never used and don't care about this feature but it's actually pretty useful! Tagged unions make representing certain types of data very elegant, for example nodes of an AST or objects in a dynamic programming language. You can use OOP patterns and dynamic dispatch to represent these things as well, but I think tagged unions are a better fit, and you get to eliminate a virtual method call by using std::visit or switching on the tag. I suspect that maybe you have never been introduced to sum types in general which is not uncommon! I am curious if you have experience with using them or not? https://en.wikipedia.org/wiki/Tagged_union |
|
I have already pointed out in some of my other replies why it is wrong to consider a variant as a replacement for POD union.