fork(2) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template <typename T> class A {
  5. string a;
  6. static T t;
  7. public:
  8. A(): a("void\n"){};
  9. operator T() { a = "T\n"; return T(a); }
  10. operator T&() { a = "ref T\n"; t = a; return t; }
  11. // operator const T&() { a = "const ref T\n"; t= a; return const_cast<const T&>(t); } (*) FIX
  12. };
  13.  
  14. template <> string A < string >::t = "zero";
  15.  
  16. int main() {
  17. string s;
  18. A<string> a;
  19. s = (string)a; // uncompilible . fixed by (*)
  20. cout << 1 << s;
  21. s = a.operator std::string(); // works
  22. cout << 2 << s;
  23. s = (string&)a; // wokrs right too
  24. cout << 3 << s;
  25. s = (const string&)a; // fails and ambigious . fixed by (*)
  26. cout << 4 << s;
  27.  
  28. return 0;
  29. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
1ref T
2T
3ref T
4ref T