fork(1) download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. namespace other {
  5. enum class Type {
  6. Type1,
  7. Type2
  8. };
  9.  
  10. std::string to_string(const Type& type) {
  11. switch(type) {
  12. case Type::Type1:
  13. return "Type1";
  14. break;
  15. case Type::Type2:
  16. return "Type2";
  17. break;
  18. default:
  19. {}
  20. }
  21.  
  22. return "Unknown";
  23. }
  24.  
  25. void run() {
  26. using namespace std;
  27. cout << string("Type: ") + to_string(Type::Type1) << endl;
  28. cout << string("int: " ) + to_string(42) << endl; // this one generates compile-time errors
  29. }
  30. }
  31.  
  32. int main() {
  33. other::run();
  34.  
  35. using namespace std;
  36. cout << string("int: " ) + to_string(42) << endl; // This one is ok
  37.  
  38. return 0;
  39. }
Compilation error #stdin compilation error #stdout 0s 3472KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'void other::run()':
prog.cpp:28:43: error: invalid initialization of reference of type 'const other::Type&' from expression of type 'int'
   cout << string("int: " )  + to_string(42) << endl;  // this one generates compile-time errors
                                           ^
prog.cpp:10:14: note: in passing argument 1 of 'std::string other::to_string(const other::Type&)'
  std::string to_string(const Type& type) {
              ^
stdout
Standard output is empty