Boost logo

Boost :

From: Fernando Cacciola (fernando_cacciola_at_[hidden])
Date: 2003-10-28 13:23:34


"E. Gladyshev" <egladysh_at_[hidden]> escribió en el mensaje
news:20031028174222.71235.qmail_at_web40806.mail.yahoo.com...
>
> 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.
>
How about not requiring a visitor overload for boost::empty?

HTH

Fernando Cacciola


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