Boost logo

Boost Users :

Subject: [Boost-users] Recursive boost::variant and/or movability?
From: Matthew L. Creech (mlcreech_at_[hidden])
Date: 2009-06-24 18:37:13


Hi,

I'm using boost::variant<> while parsing an input file to create a
AST. The example at:

http://www.boost.org/doc/libs/1_39_0/doc/html/variant/tutorial.html#variant.tutorial.recursive.recursive-wrapper

does something very similar. However, it seems that the example usage
of boost::recursive_wrapper<> is inefficient, not because of the
wrapper itself, but because the creation of a tree of variants becomes
more and more costly as that tree grows.

The attached example is similar to the tutorial. Creating a nested
tree 10,000 deep (with no branching) causes more than 100,000,000
copy-constructions. I.e. since there's no move-awareness, for each
single leaf that gets added to the tree we end up creating a new node,
adding the leaf to it, then recursively copying over the entire
sub-tree up to that point.

Creating a move-constructor for the variant type didn't do any good
(it was never invoked). But several discussions online (including
notes from the original review) imply that boost::variant<> is
move-aware. Is this the case? If so, does anyone know how I can get
move semantics working using boost::recursive_wrapper<>?

I also tried changing the variant type(s) to std::unique_ptr<> rather
than boost::recursive_wrapper<>, but that didn't work either (build
errors due to non-copyability, specifically in
"variant/variant.hpp":404).

This is Boost 1.39, and I'm building with GCC 4.4 in C++0x mode.
Thanks for the help!

-- 
Matthew L. Creech

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net