[variant] passing visitors to apply_visitor()

Hi. When I want to apply a visitor to a variant, I currently have 2 choices: boost::apply_visitor( my_visitor(), my_variant ); and my_visitor mv; my_variant.apply_visitor( mv ); A tempting 3rd choice of my_variant.apply_visitor( my_visitor() ); doesn't work because the variant::apply_visitor member function accept a const visitor ref, which means it can't be a temporary object. Moreover, the first option (boost::apply_visitor freestanding function) also has a problem - it only accepts non-const variants. I'm not really sure, but I guess that all these limitation are originated from the desire to pass the visitor by ref, and it's not always possible to provide both const-ref and non-const-ref overloads. My question is: why by reference and not by value? I see visitors as a kind of function objects, and I think that's how it was meant to be seen. Function objects are usually passed by value, so why should visitors be any different? I couldn't find anything relevant to this in the docs. Passing by value will allow the much more appealing syntax I presented as the 3rd option, and it will remove the limitation from the freestanding function. Makes sense or complete nonsense? Thanks, Yuval
participants (1)
-
Yuval Ronen