Chris Glover wrote:
That's hard to believe. Copying a pointer should be insignificant compared to the cost of the dispatch. What does the profiler say?
Which compiler? I have found that MSVC is extremely pessimistic with this type of situation and generates unnecessary mov instructions for the pointer in the struct.
It's inside a switch on the type index. A mov instruction or two shouldn't matter. The generated code should be something like switch( v.which() ) { case 0: my_visitor( get<T1>(v) ); break; case 1: my_visitor( get<T2>(v) ); break; // and so on } except with about seven layers of templates on top. I just don't see an extra pointer copy being of significance here. If the switch is compiled to a table you have an indirect jmp; if to a sequence of comparisons, would depend on how well they can be predicted.