I have an enum class of types and want a "to_string" function for outputting the type name so I wrote that inside my own namespace. Problem is, other functions in that namespace trying to call to_string on, e.g., an int (really just an int, not intended to be part of the enum) are finding the custom to_string and giving errors about invalid initialization of the enum.
I know I could explicitly call std::to_string instead of to_string but I assume there's a better way. What am I doing wrong?
Here is example code:
#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;
}