fork(2) download
  1. #include <iostream>
  2. #include <string>
  3. #include <boost/variant.hpp>
  4.  
  5. template <typename T>
  6. struct conversion_traits
  7. {
  8. typedef T type;
  9. };
  10.  
  11. template <>
  12. struct conversion_traits<const char*>
  13. {
  14. typedef std::string type;
  15. };
  16.  
  17. template <template <typename> class conv_traits, typename... Types>
  18. struct my_variant : public boost::variant<Types...>
  19. {
  20. using boost::variant<Types...>::variant;
  21.  
  22. template <typename T>
  23. auto operator=(T rhs)
  24. {
  25. return boost::variant<Types...>::operator=(static_cast<typename conv_traits<T>::type>(rhs));
  26. }
  27. };
  28.  
  29. int main()
  30. {
  31. my_variant<conversion_traits, std::string, void const*> foo;
  32. foo = "bar";
  33. std::cout << foo.which();
  34. return 0;
  35. }
Success #stdin #stdout 0s 3280KB
stdin
Standard input is empty
stdout
Standard output is empty