fork download
  1. #include <string>
  2. #include <typeinfo>
  3. using namespace std;
  4. template <typename T>
  5. static T StringCast(string inValue)
  6. {
  7. if(typeid(T) == typeid(bool))
  8. {
  9. if(inValue == "true")
  10. return true;
  11. if(inValue == "false")
  12. return false;
  13. if(inValue == "1")
  14. return true;
  15. if(inValue == "0")
  16. return false;
  17. return false;
  18. }
  19.  
  20. std::istringstream stream(inValue);
  21. T t;
  22. stream >> t;
  23. return t;
  24. }
  25.  
  26. class Fail {
  27. public:
  28. };
  29. std::istream& operator >> (std::istream& is, Fail&) { return is; }
  30.  
  31. int main()
  32. {
  33. Fail f;
  34. f = StringCast<Fail>("HA");
  35. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘T StringCast(std::string) [with T = Fail]’:
prog.cpp:34:   instantiated from here
prog.cpp:10: error: conversion from ‘bool’ to non-scalar type ‘Fail’ requested
prog.cpp:12: error: conversion from ‘bool’ to non-scalar type ‘Fail’ requested
prog.cpp:14: error: conversion from ‘bool’ to non-scalar type ‘Fail’ requested
prog.cpp:16: error: conversion from ‘bool’ to non-scalar type ‘Fail’ requested
prog.cpp:17: error: conversion from ‘bool’ to non-scalar type ‘Fail’ requested
prog.cpp:20: error: ‘stream’ has incomplete type
stdout
Standard output is empty