3 ms·
> Sum types don't just store values of different types. That's exactly what a tagged union does. std::variant is a tagged union. I was not aware of the name 's
by clishem 9y ago
> Sum types don't just store values of different types.
That's exactly what a tagged union does. std::variant is a tagged union. I was not aware of the name 'sum type' but it's supposed to be a synonim for a tagged union. Guess not, but std::variant is not meant as what you describe[1].
[1]: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4542.pdf http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n454...
- JoshTriplett 9y ago> That's exactly what a tagged union does. std::variant is a tagged union. Not exactly. std::variant is one particular type of a tagged union, where the only discriminant is the type. You can also have a tagged union where the discriminant determines some semantic state, and multiple such states may store the same type of value. That's still a sum type, still a tagged union, and not something std::variant can do.
- clishem 9y agoThanks for the clarification. This article also helped. http://talesofcpp.fusionfenix.com/post-21/rant-on-the-stdexperimentalvariant-to-come http://talesofcpp.fusionfenix.com/post-21/rant-on-the-stdexp... In it's terminology, std::variant is a 'discriminated union' instead of a 'sum type'. Making std::variant the latter would also have had disadvantages it seems.