fork download
  1. #include <iostream>
  2. #include "boost/variant.hpp"
  3.  
  4. struct A {};
  5. struct B {};
  6.  
  7. typedef boost::variant<A, B> AorB;
  8.  
  9. void foo(A&) { std::cout << "foo(A&)\n"; }
  10. void foo(B&) { std::cout << "foo(B&)\n"; }
  11.  
  12. int main()
  13. {
  14. A a;
  15. AorB c;
  16. c = a;
  17. foo(boost::get<A>(c));
  18. try
  19. {
  20. foo(boost::get<B>(c));
  21. }
  22. catch(boost::bad_get& e)
  23. {
  24. std::cout << e.what();
  25. }
  26. }
Success #stdin #stdout 0.01s 2904KB
stdin
Standard input is empty
stdout
foo(A&)
boost::bad_get: failed value get using boost::get