Boost logo

Boost :

Subject: Re: [boost] [Variant] C++11 variadic template implementation
From: Larry Evans (cppljevans_at_[hidden])
Date: 2012-04-15 13:39:25

On 04/15/12 04:57, Florian Goujeon wrote:
> Hi Boosters,
> I'd like to know if anybody were working on a C++11 version (using
> templates) of Boost.Variant.
> I currently use the 'classic' version in my project, but since I need
> more-than-20-type variants, the compilation take a lot of time
> (extending the
> number of types that variant can support forces you to disable the use of
> precompiled headers). Plus, I'll need to use 70 type variants (MPL lists
> are
> physically limited to 50 types).
> If nobody has started to implement such a version yet, I'd be pleased to
> contribute.
> I've written an implementation. It's incomplete and far from perfect,
> but it
> could (hopefully) be a viable working basis.
> _______________________________________________
> Unsubscribe & other changes:
There's this:

which uses variadic templates; hence, doesn't have the limitations you
mentioned about the number of bounded types.

*HOWEVER* I'v not tested whether it takes more or less compile time
 than variant.

One difference of one_of_maybe w.r.t. boost::variant is that it's a
true tagged variant. IOW, it allows duplicates in the bounded types:

For example:


is not allowed because variant<int,int> when assigned from an int:

  variant<int,int> ii;

wouldn't, IIRC, know which bounded type, the 1st int or the 2nd, was
intended as the target.


  container<tags::one_of_maybe,unsigned,int,int> ii;

would work because the tag (the template arg to inject member function),
indicates which bounded type is intended.

You might object that:

  ii = int(999);

is less "user-friendly" than:


however, I thought making the structure more like the actual
mathematical disjoint union:

was worth the extra trouble.

In addition, one_of_maybe certainly doesn't have all the nice
features of variant outlined here:

and in particular, it does not have:

Instead, if one of the bounded types has not yet been injected, the
which function will return a tag value indicating this and the user is
required to check this. I thought this would be useful for
implementing something like boost's optional, which would then be like
one_of_maybe with just 1 bounded type.

Tests for one_of_maybe (and other composite_storage types) are here:



Boost list run by bdawes at, gregod at, cpdaniel at, john at