NoS*_*tAl 17 c++ enums tostring
C++没有办法获取枚举的字符串表示.人们解决这个问题通过编写又名含有大量的样板代码自定义函数
switch
与case XYZ return "XYZ";
那当然要求枚举的用户知道自定义函数的名称.
所以我想我可以添加一个专门化std::to_string
来让用户to_string
在我的枚举上使用.像这样的东西:
//
#include <iostream>
#include <string>
#include <cassert>
#define TEST
class Car
{
public:
enum class Color
{
Red,
Blue,
White
};
};
#ifdef TEST
#include <string>
namespace std
{
std::string to_string (Car::Color c)
{
switch (c)
{
case Car::Color::Red:
return "Red";
case Car::Color::Blue:
return "Blue";
case Car::Color::White:
return "White";
default:
{
assert(0);
return "";
}
}
}
}
#endif
int main()
{
std::cout << std::to_string(Car::Color::White) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
这个解决方案有什么问题吗?
Jon*_*ely 22
这不是"覆盖"(适用于virtual
函数),并且您没有添加"专门化"(适用于模板),您添加了一个重载,它将一个新函数的声明和定义添加到命名空间std
,这是禁止:
17.6.4.2.1命名空间std [namespace.std]
如果C++程序向命名空间std
或命名空间中的命名空间添加声明或定义,则它是未定义的,std
除非另有说明.只有当声明取决于用户定义的类型并且特化符合原始模板的标准库要求且未明确禁止时,程序才可以将任何标准库模板的模板特化添加到命名空间std.
更好的解决方案是在您自己的命名空间中重载它,而to_string(c)
不是调用std::to_string(c)
.这将找到正确的功能,你不需要添加任何东西std