Boost logo

Boost :

From: dan marsden (danmarsden_at_[hidden])
Date: 2006-05-06 03:59:32


Gennadiy Rozental wrote:
>I may not have time for review, but just one simple question: what are the
>advantages/disadvantages/differences in usage with
>stl_container<boost::variant>?

Disclaimer: I'm not a regular user of boost::variant.

I believe the key differences is what information is available / fixed at compile time
rather than run time. So considering an example from the variant docs:
std::vector< boost::variant<int, std::string> > vec;
vec.push_back( 21 );
vec.push_back( "hello " );Here the actual types of the elements of vec would have to be tested at run time, and
the length of vec can be varied at run time by pushing back more elements. Iterating over
the sequence in algorithms will require runtime overhead to manipulate the iterators,
compare them for equality etc.

Compare this with:

boost::fusion::vector<int, std::string> vec2(0, "hello world");

Here the types of vec2 are fixed and available at compile time. In fact
boost::fusion::vector<int, std::string> is an MPL sequence. The length of
vec2 is also fixed at compile time, pushing back another element on to a fusion sequence
will actually result in a sequence of a different type. Iterating over vec2 using the fusion
algorithms should not incur significant runtime cost (assuming sufficient inlining from
the compiler used). Having the type and length information available to the compiler may
help to automatically pick up certain classes of bugs that may only show up at runtime
with the container of variants strategy.

I hope that helps discriminate between the 2 approaches.
Thanks
Dan


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