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
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 :)