Boost logo

Boost Users :

From: Chris Russell (cdr_at_[hidden])
Date: 2007-01-19 11:04:55


Thanks Yuval - this allows me to do exactly what I want.

It would be useful to add this information to the high-level class overview
http://boost.org/doc/html/variant/reference.html#header.boost.variant.hpp
and to the Basic Usage tutorial section
http://boost.org/doc/html/variant/tutorial.html#variant.tutorial.basic which
doesn't mention this useful detail.

To your point about passing the visitor by value: I don't understand why
this would be useful. One could for example simply call like:

typedef boost::variant<int,long,string> my_variant;
class my_visitor : public boost::static_visitor<>
{ //... };
my_variant v(5);
v.apply_visitor(my_visitor());

... which constructs the visitor as a temporary but doesn't incur the
overhead of an extra copy constructor.

Anyway - thanks very much for pointing out that I can call apply_visitor
directly on the variant. This saves me time and makes my code more readable
than the previous suggestion of using boost::bind to adapt the unary visitor
functors into binary functors that accept a non-const reference to the data
I want to operate on in my visitor's operator().

- Regards

Chris

"Yuval Ronen" <ronen_yuval_at_[hidden]> wrote in message
news:eoq76s$is8$1_at_sea.gmane.org...
> Accepting the visitor by non-const reference will make calling
> apply_visitor with a temporary visitor not compile, so there's a
> downside to that too. You can use the apply_visitor member function of
> variant:
>
> v1.apply_visitor(my_visitor);
> v2.apply_visitor(my_visitor);
>
> which accepts the visitor by non-const reference.
>
> Not that I'm saying that any if these solutions is best. IMO, a
> static_visitor is some kind of a functor, and as such should be accepted
> by value, making it possible to pass temporaries, and write code as you
> described. However, I couldn't convince about it...
>
> Yuval


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