Standardizing Variant: Difficult Decisions -- Anthony Williams

Techincal discussion information while standardizing N4542.

Standardizing Variant: Difficult Decisions

by Anthony Williams

From the article:

One of the papers proposed for the next version of the C++ Standard is N4542: Variant: a type safe union (v4). As you might guess from the (v4) in the title, this paper has been discussed several times by the committee, and revised in the light of discussions.

Boost has had a variant type for a long time, so it only seems natural to standardize it. However, there are a couple of design decisions made for boost::variant which members of the committee were uncomfortable with, so the current paper has a couple of differences from boost::variant. The most notable of these is that boost::variant has a "never empty" guarantee, whereas N4542 proposes a variant that can be empty.

Add a Comment

Comments are closed.

Comments (1)

0 0

Piotr said on Sep 5, 2015 07:39 AM:

I was using Boost::variant for a long time for several reasons as a base class for my universal data type (for JSON/XML).
After some time I needed few more things in this solution, so I created a new class able to storing (almost) any data type.

It's a mixture of boost::variant (fixed number of data types) and boost::any (any type).

Pros:
- handles "null" type as a special state of variable
- allows automatic conversion between types (for example read string as int)
- can work as a object holder (I needed this in previous versions, similar to smart pointer)
- allows storing any built-in scalar value, class & structure without writing a single line of extra code

Cons:
- it's possible it's not as clean as boost solutions
- code is just published, so I assume a lot of issues can be there

See: http://bit.ly/1LQu4FE