Question

Consider:

typedef boost::variant<T0, ..., TN> variant_T_t;
typedef boost::variant<U0, ..., UN> variant_U_t;
...
typedef boost::variant<variant_T_t, variant_U_t, ...> variant_t;

This extends the limit on the number of types that can be held by my type to pow(BOOST_VARIANT_LIMIT_TYPES, L), where L is the number of levels nested.

Is this (to an extent) an acceptable solution, or is it just a bad hack? Is there a better solution out there? Perhaps the old-fashioned union is more appropriate?

Was it helpful?

Solution 2

It's an ugly hack. It will make your visitors or your get calls extremely ugly and unreadable.

Try just increasing the limit instead - Boost.Variant defines it to be the same as BOOST_MPL_LIMIT_LIST_SIZE, which you can override if you disable the pre-generated headers (which is a hit in compile time, though).

http://www.boost.org/doc/libs/1_53_0/libs/mpl/doc/refmanual/limit-list-size.html

Why do you need this crazy amount of different types anyway? This sounds like a potential design issue.

OTHER TIPS

As of boost 1.57, boost variant can be created via boost::make_variant_over, which takes a type Sequence as a type parameter. There is no type limitation on that type until that models boost::mpl's Sequence Concept.

The vanilla boost::mpl::vector typelist can be used up to 50 types. Though if you have a C++11 compliant compiler, then it is not so hard to create your own variadic typelist and a glue code for that, which makes that to be an MPL Sequence. And from this point, the number of types is limited by the compiler's allowed maximum template instantiation depth, which is usually 256.

In this GIST, I am taking Dave Abraham's typelist and adding the glue code, so it models mpl's sequence concept. Note, that I'have not done exhausting testing on several compilers, also I just tested it with boost 1.56.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top