Boost logo

Boost :

From: james.jones_at_[hidden]
Date: 2006-09-06 19:10:18

From: Yuval Ronen <ronen_yuval_at_[hidden]>
> Also notice the third operator(): if you wanted the opposite - convert
> the int to float and store the plus result in the variant, thus changing
> the type of the variant's content, then I don't know how to do it. I
> mean, you could make the visitor derived from static_visitor<float>,
> make the operators() return the result, and store it in the variant
> later, but that would also make int+int result a float, which might be
> not good for you...

I had the same problem with a visitor that I wanted to mutate the variant type. The solution I came up with was to wrap the variant in a class, then define my various methods in the class and forward them to the variant, then assign the result back to the class, like so:

typedef [...whatever...] private_variant_type;

class variant_type
        private_variant_type _value;

        operator+= (const variant_type& lhs, const variant_type& rhs)
                _value = boost::apply_visitor(addition_visitor(), lhs, rhs);
                return *this;

The addition_visitor class returns private_variant_type.

I think this is a little clunky, but it DOES work. It would be great if there were a way to change the type of a variant from within a visitor!

James Jones Administrative Data Mgmt.
Webmaster 375 Raritan Center Pkwy, Suite A
Data Architect Edison, NJ 08837

Boost list run by bdawes at, gregod at, cpdaniel at, john at