Boost logo

Boost Users :

From: Yuval Ronen (ronen_yuval_at_[hidden])
Date: 2007-01-19 12:27:44


Chris Russell wrote:
> 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());

This won't compile - as I said, you can't pass a temporary as an
argument to a function that takes its argument by non-const reference
(in a conforming compiler, that is).

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

The copy constructor should be a non-issue, just as is the case with
functors. Why don't you complain that std::for_each accept its functor
by value? Because copy construct of a functor should be trivial - copies
references at most. static_visitors are just like functors, IMO.

> 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().

You're welcome :)


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