Boost logo

Boost :

From: E. Gladyshev (egladysh_at_[hidden])
Date: 2003-10-28 12:42:22


The recommended method to optimize variant is to add boost::empty
(or any non-throw copy-constructible type) to the list of variant types.
This will make sure that variant won't allocate/deallocate
memory during assignments. Several people pointed out that in
a generic context, this solution may be problematic.

I'd like to give an example of that.

//variant visitor
struct my_variant_visitor : static_visitor
{
        template< typename T >
        template()( T& x )
        {
                x.f(); //** Here is the catch! We require that all variant types have f()
        }
};

struct my_type1()
{
        void f();
};

struct my_type2()
{
        void f();
};

variant<my_type1, my_type2> v;
apply_visitor( my_variant_visitor(), v ); //Ok

** Now we try to optimize the variant by adding boost::empty.

variant<boost::empty, my_type1, my_type2> v;
apply_visitor( my_variant_visitor(), v ); //Error: boost::empty doesn't have f().

Am I missing something? It seems like a serious problem that
makes such optimization almost impossible in a lot of my code.
Is there any way to solve it nicely.

Eugene

__________________________________
Do you Yahoo!?
Exclusive Video Premiere - Britney Spears
http://launch.yahoo.com/promos/britneyspears/


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