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

Standardizing Variant: Difficult Decisions

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

56 comments sorted by

View all comments

Show parent comment

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.