fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <type_traits>
  4.  
  5. using namespace std;
  6.  
  7. template<typename U>
  8. class A
  9. {
  10. public:
  11. static_assert(!std::is_same<int, U>::value && !std::is_same<double,U>::value,
  12. "Template argument cannot be <int> or <double>.");
  13. A(const U& u) : u(u) {};
  14. explicit operator U() { return u; }
  15. explicit operator int() { return i; }
  16. explicit operator double() { return d; }
  17.  
  18. private:
  19. U u;
  20. int i = 5;
  21. double d = 3.14;
  22. };
  23.  
  24. int main()
  25. {
  26. A<string> a("name");
  27. cout << double(a) << '\n';
  28. cout << int(a) << '\n';
  29. cout << string(a) << '\n';
  30. return 0;
  31. }
  32.  
Success #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
3.14
5
wow