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

5

u/eric_niebler Jun 17 '15

I don't like that the first typename has a special role.

Do you feel the same about built-in unions?

having an empty state that can occurr, but only rarely and we kinda try to avoid and discourage it but not completely, is a bad thing.

I'm sure you would agree that swallowing exceptions and continuing as if nothing went wrong is a bad thing. Should we compromise the design of our vocabulary types -- complicating their use everywhere and for everyone -- to accommodate bad code? No.

There is no empty state. There are invalid variants that can only be encountered in bad code.

1

u/suspiciously_calm Jun 18 '15

Do you feel the same about built-in unions?

Yes.

I'm sure you would agree that swallowing exceptions and continuing as if nothing went wrong is a bad thing. Should we compromise the design of our vocabulary types -- complicating their use everywhere and for everyone -- to accommodate bad code?

It doesn't complicate use, since a variant holding the wrong type is also a real possibility, and it doesn't complicate the design of the type either, since the "invalid state" representation has to be there anyway. The only difference is whether the default constructor constructs an empty variant or one of the variant types which is selected by some arbitrary rule.