r/cpp • wx | soci | swig • Jun 17 '15

Standardizing Variant: Difficult Decisions

https://www.justsoftwaresolutions.co.uk/cplusplus/standardizing-variant.html
32 Upvotes

56 comments sorted by

View all comments

Show parent comment

0

u/foonathan Jun 18 '15

Do you really want nothing to happen when you visit a variant that has been made invalid by a failed move? Nothing?

No, I want nothing to happen, when visiting an empty variant. And a variant can get empty after being default constructed, manually reset or as a fallback for a failed move.

And you notice a failed move because an exception is thrown. But with a variant which is allowed to be empty, it is safe to access it after the exception without harm. Because it has a valid, well-defined state: it is empty.

3

u/eric_niebler Jun 18 '15

This behavior is easily implemented in terms of the variant currently proposed and optional with some trivial forwarding functions.

-1

u/foonathan Jun 18 '15

Of course it is. I was just argueing against your point that an empty state leads to conditionals in user code. This is not the case (idiomatic use taken for granted).

2

u/cdglove Jun 19 '15

No, I want nothing to happen, when visiting an empty variant. And a variant can get empty after being default constructed, manually reset or as a fallback for a failed move.

Variant is a value type. Value types should never have a valid empty state. What you're saying applies to reference types and is more akin to Java style null checks. I really don't want that. If you do, however, you can implement it yourself in terms of the current variant by making your own 'none' type. The same is not true for the reverse; if variant is default constructed as empty I am forced to check everywhere because empty has been promoted to a valid state. In my opinion it's generally bad design to force users to use a library is a certain way like this.