#include <iostream>
#include <string>
namespace other {
enum class Type {
Type1,
Type2
} ;
std:: string to_string( const Type& type) {
switch ( type) {
case Type:: Type1 :
return "Type1" ;
break ;
case Type:: Type2 :
return "Type2" ;
break ;
default :
{ }
}
return "Unknown" ;
}
void run( ) {
using namespace std;
cout << string( "Type: " ) + to_string( Type:: Type1 ) << endl;
cout << string( "int: " ) + to_string( 42 ) << endl; // this one generates compile-time errors
}
}
int main( ) {
other:: run ( ) ;
using namespace std;
cout << string( "int: " ) + to_string( 42 ) << endl; // This one is ok
return 0 ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgoKbmFtZXNwYWNlIG90aGVyIHsKCWVudW0gY2xhc3MgVHlwZSB7CgkJVHlwZTEsCgkJVHlwZTIKCX07CgkKCXN0ZDo6c3RyaW5nIHRvX3N0cmluZyhjb25zdCBUeXBlJiB0eXBlKSB7CgkJc3dpdGNoKHR5cGUpIHsKCQkJY2FzZSBUeXBlOjpUeXBlMToKCQkJCXJldHVybiAiVHlwZTEiOwoJCQkJYnJlYWs7CgkJCWNhc2UgVHlwZTo6VHlwZTI6CgkJCQlyZXR1cm4gIlR5cGUyIjsKCQkJCWJyZWFrOwoJCQlkZWZhdWx0OgoJCQl7fQoJCX0KCQkKCQlyZXR1cm4gIlVua25vd24iOwoJfQoJCgl2b2lkIHJ1bigpIHsKCQl1c2luZyBuYW1lc3BhY2Ugc3RkOwoJCWNvdXQgPDwgc3RyaW5nKCJUeXBlOiAiKSArIHRvX3N0cmluZyhUeXBlOjpUeXBlMSkgPDwgZW5kbDsKCQljb3V0IDw8IHN0cmluZygiaW50OiAiICkgICsgdG9fc3RyaW5nKDQyKSA8PCBlbmRsOyAgLy8gdGhpcyBvbmUgZ2VuZXJhdGVzIGNvbXBpbGUtdGltZSBlcnJvcnMKCX0KfQoKaW50IG1haW4oKSB7CglvdGhlcjo6cnVuKCk7CgkKCXVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgljb3V0IDw8IHN0cmluZygiaW50OiAiICkgICsgdG9fc3RyaW5nKDQyKSA8PCBlbmRsOyAgLy8gVGhpcyBvbmUgaXMgb2sKCglyZXR1cm4gMDsKfQ==
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