fork download
  1. #include <string>
  2.  
  3. class C
  4. {
  5. std::string p_String = "";
  6. bool p_Bool = false;
  7. int p_Int = 0;
  8. public:
  9. template <typename T>
  10. operator T() const
  11. {
  12. if constexpr (std::is_same<T, bool>::value)
  13. return p_Bool;
  14. if constexpr (std::is_same<T, std::string>::value)
  15. return p_String;
  16. if constexpr (std::is_same<T, int>::value)
  17. return p_Int;
  18. }
  19. };
  20.  
  21. int main()
  22. {
  23. C instance;
  24. bool b = static_cast<bool>(instance);
  25. int i = static_cast<int>(instance);
  26. std::string s = static_cast<std::string const&>(instance);
  27. //std::string s = static_cast<std::string>(instance);
  28. }
  29.  
Success #stdin #stdout 0s 5008KB
stdin
Standard input is empty
stdout
Standard output is empty