r/cpp • u/_VZ_ wx | soci | swig • Jun 17 '15
Standardizing Variant: Difficult Decisions
https://www.justsoftwaresolutions.co.uk/cplusplus/standardizing-variant.html
31
Upvotes
r/cpp • u/_VZ_ wx | soci | swig • Jun 17 '15
View all comments
Show parent comment
2
u/[deleted] Jun 18 '15 edited Jun 18 '15
How can I default construct a variant of non-default constructible types without an empty/invalid state? IIUC at least one type must be default constructible for this to be possible.
That's sad! The OP mentioned it briefly:
I don't want empty variants but I want default constructible variants. When should a variant be default constructible? If
and why?
After giving this some thought, all options suck a bit. In the mathematical discriminated union that the variant is trying to model, the order of the types does not matter. I am getting more inclined to eliminate element access by index and allow only access by type, which is possible if there are no duplicated types. Then, the variant should try as hard as possible to be default constructible, that is, be default constructible if any of the types it contains can be default constructed. Which type it will be default constructed should be unspecified, if the user didn't care to default construct a particular type, then that is just the way it is.
In particular, I would hate to see variant wrappers or generic code in the wild that reorders the element of the variant just so that it can be default constructible.
TIL this. That's really unfortunate! These have to be left in a valid state and this requires sentinel heap allocated nodes so I don't see a way for them to provide a noexcept move constructors.