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
variadic
>
> 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:
> http://lists.boost.org/mailman/listinfo.cgi/boost
>
There's this:

http://svn.boost.org/svn/boost/sandbox/variadic_templates/boost/composite_storage/pack/container_one_of_maybe.hpp

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:

http://www.boost.org/doc/libs/1_49_0/doc/html/variant/tutorial.html#variant.tutorial.basic

For example:

  variant<int,int>

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

  variant<int,int> ii;
  ii=int(999);

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

OTOH:

  container<tags::one_of_maybe,unsigned,int,int> ii;
  ii.inject<0>(999);

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:

  ii.inject<0>(999);

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

 http://en.wikipedia.org/wiki/Disjoint_union

was worth the extra trouble.

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

http://www.boost.org/doc/libs/1_49_0/doc/html/variant.html#variant.abstract

and in particular, it does not have:

http://www.boost.org/doc/libs/1_49_0/doc/html/variant/design.html#variant.design.never-empty

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:

http://svn.boost.org/svn/boost/sandbox/variadic_templates/libs/composite_storage/sandbox/pack/composite_storage.leaf.test.cpp

HTH.

-regards,
Larry


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk