fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. #include <boost/variant.hpp>
  5.  
  6. void foo( boost::variant<int,long,std::string> arg )
  7. {
  8. using namespace std;
  9.  
  10. cout << "foo: ";
  11. switch( arg.which() ) {
  12. case 0:
  13. cout << "<int> " << boost::get<int>(arg) << endl;
  14. break;
  15. case 1:
  16. cout << "<long> " << boost::get<long>(arg) << endl;
  17. break;
  18. case 2:
  19. cout << "<string> " << boost::get<std::string>(arg) << endl;
  20. break;
  21. }
  22. }
  23.  
  24. int main() {
  25.  
  26. foo( 1 );
  27. foo( 2L );
  28. foo( std::string("hello world") );
  29. //foo( 3u ); // compile error
  30. }
  31.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty