
Hello, #include <iostream> #include <boost/variant/variant.hpp> typedef boost::variant<int, double, char> VT; struct print_type : public boost::static_visitor<const char*> { const char* operator()( int ) { return "int"; } const char* operator()( double ) { return "double"; } const char* operator()( char ) { return "char"; } }; int main() { VT v('c'); std::cout<< v.apply_visitor( print_type() ) <<std::endl;; } If I understood the doc correctly, the visitation is supposed to be compile-time, that is, v.apply_visitor( print_type() ) is already "char" before the program start... Is this right? If so, is there a way to see this in the binary executable file? Maybe viewing symbols? I have access to vs2005 and g++3/4 /linux platforms. It would be so fantastic if compilers output _also_ the C++ source resulting from the template machinery! Best regards,

If I understood the doc correctly, the visitation is supposed to be compile-time, that is, v.apply_visitor( print_type() ) is already "char" before the program start... Is this right?
The visitation is "compile-time" in the sense that it's type-safe. "v.apply_visitor( print_type() )" might or might not be substituted by "char" depending on your optimization switches. In MSVC9, with optimization (/O2) it produces the following output: 00402B3C push offset string "char" (419368h) 00402B41 push offset std::cout (420BC8h) 00402B46 call std::operator<<<std::char_traits<char> > (4027F0h) So "v.apply_visitor( print_type() )" is really substituted by "char" in compile-time. But without the optimization you'll go through all the visitation calls.
If so, is there a way to see this in the binary executable file? Maybe viewing symbols?
In MSVC - compile the program, step into main (or set breakpoint), then go to Debug-->Windows-->Dissasembly.
participants (2)
-
Hicham Mouline
-
Igor R