r/cpp • u/_VZ_ wx | soci | swig • Jun 17 '15
Standardizing Variant: Difficult Decisions
https://www.justsoftwaresolutions.co.uk/cplusplus/standardizing-variant.html
32
Upvotes
r/cpp • u/_VZ_ wx | soci | swig • Jun 17 '15
21
u/eric_niebler Jun 17 '15 edited Jun 17 '15
I was at the committee meeting where this was discussed, and I argued strongly in favor of the current design. The problem with default-constructing to an "empty" state is that your programs are now replete with empty variants. It is promoting the empty state to a valid state. This complicates code every time a variant is used. You must now sprinkle your code with ASSERTs or conditionals. That's really undesirable.
By default-constructing to the first element in the variant (like built-in unions do), the only source of "empty" variants will be throwing moves. Not only are these rare, but since it's an exception, you have already been notified that your variant is now invalid. Yes, if you swallow the exception and proceed as if nothing has gone wrong, then you can end up accessing an invalid variant. The answer is simple: DON'T SWALLOW EXCEPTIONS. You shouldn't be doing that, anyway.
The design the committee is currently looking at makes it possible to code as if a
variant<int, string>can only have anintor astring, without paying for the never-empty guarantee.Notice the changed terminology. The current design doesn't have an "empty" state. It has an invalid state. I don't consider that mere sophistry. If you don't swallow exceptions, you can have your cake and eat it.
11
u/F-J-W Jun 17 '15
Either it should never be empty, or empty should be a relatively normal thing. It being a very rare thing just creates bugs because people will ignore it.
Regarding the solutions: One is missing: require noexcept move-ctor and do a three step assignment:
tby copying the new value (may throw)t. (noexcept per requirement)That aside: I believe that a default-ctor that creates an instance of one of the many arguments is a bad-idea. Make that ill-formed and maybe offer to pass in an instance of std::integral_constant<std::size_t, N> that tells the ctor to default-construct the N'th value.
Regarding a tag-type to allow empty states if they are not always allowed, I believe that we already have that “type” in the core-language:
voidI think it is obvious what it would mean and it represents a clean solution.
11
u/david_sankel Jun 17 '15
I think the current std variant design is about as close to ideal as we can get with C++. I was also at the meeting where this was discussed and was strongly in favour of the design.
std::tuple is an attempt to mimic the mathematical cross product. In other words, given types A and B, I want a std::tuple<A,B> to have a value of type A and a value of type B. No one would argue that std::tuple<A,B> should also have another state called "empty" where it contains neither a value of type A nor a value of type B. The same reasoning works with std::variant when we see what it is attempting to model.
std::variant is an attempt to mimic the mathematical discriminated union. In other words, given types A and B, I want a std::variant<A,B> to have a value of type A or a value of type B. Again, a state called "empty" makes little sense given what we are trying to model.
std::variant has an invalid state because of the strange C++-ism of throwing copy-constructors. An invalid state certainly isn't desirable, but it beats all the alternatives. The case where it arises is very rare and, because an exception is thrown, the programmer is already informed that there is a problem and can deal with it. The end result is that we get a std::variant type that we can safely assume is valid when passed as a parameter to a function. We make the same validity assumption when we skip null checks for references passed to functions.
2
u/[deleted] Jun 19 '15
Yes, this is exactly the rule that is now in place for get<T>(tuple&): the program is ill-formed unless T uniquely selects a component, and IMO it is a good rule. In all other cases you can use the same type-based indexing that was in v2. If it is indeed a niche application, the number of cases where numeric-indices are used will be similarly small.
I don't expect variant<T,T> to be a common type at API boundaries, but I might want it as intermediate within a function. Its the same as a T and bool basically, so I'm thinking of the case of a "control" variable in a loop where I might want the option to rewrite it with a variant.
Also, this wish about visitors.
1
u/no1msd Jun 18 '15
Slightly offtopic, but can anyone tell me what do these voting results mean? SF=0 WF=0 N=8 WA=4 SA=2
1
u/Elelegido Jun 20 '15
Maybe returning optionals could be better if emptiness is allowed.
0
u/vlovich Jun 17 '15
I agree that default construction should leave it in an empty state. Since the empty state is unavoidable, then maybe it's OK for a copy-exception to leave it in the empty state.
The empty state should explicitly be an invalid state where any attempted operation throws including copy, copy-assignment, etc.
I would be interested to know when the destroy after copy-complete state can cause issues. Is this something that can be detected & unsafe values rejected at compile-time? I'm also curious why allocating a temporary if the type you are copying is not noexcept copy constructible is such an undesirable behaviour that it's worth it to introduce potentially unwieldy API semantics rather than take the hit of allocating on the heap.
0
u/[deleted] Jun 17 '15
[deleted] — view deleted comment
0
u/[deleted] Jun 18 '15
[deleted] — view deleted comment