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