这里有一些枚举类:
enum class Race : char {AINU, ELF, DWARF, MAN, EAGLE, HOBBIT, ENT, ORC, WIZARD};
enum class Color: char {RED, ORANGE, YELLOW, GREEN, BLUE, PURPLE};
enum class Direction: char{UP, DOWN, LEFT, RIGHT};
我想为每个实现一个enum_to_string 函数 和一个string_to_enum 函数。
将枚举转换为字符串没有问题,因为我可以重载相同的函数名。
std::string to_string(Race const& enum_value);
std::string to_string(Color const& enum_value);
std::string to_string(Direction const& enum_value);
但是,在转换为枚举时不能以相同的方式重载,因为只有返回类型不同。 (我也不想,因为不同的枚举可能由相同的字符串表示。)
是否可以通过以下方式之一将字符串转换为枚举?
Race race = to_enum<Race>("elf");
Color color = to_enum<Color>("green");
std::string blah{"up"};
Direction dir{to_enum<Direction>(blah)};
或者可能:
Race race = to_enum(Race,"elf");
Color color = to_enum(Color,"green");
std::string blah{"up"};
Direction dir{to_enum(Direction,blah)};
C++ 能否支持其中一种或两种行为?
我试图避免这样的不同函数名称:
Race to_race(std::string const& str);
Color to_color(std::string const& str);
Direction to_direction(std::string const& str);
这是我能想到的最接近的东西,
template <typename T>struct to_enum{};
template <>
struct to_enum<Color>{
static Color convert(std::string const& str){
//match the string with a color enum, and return that color enum
//(or like a default color enum if the string is garbage or something)
}
};
然后你这样调用它:
Color color = to_enum<Color>::convert("red");
我们可以摆脱转换吗?或者可能实现这个?
Color color = to_enum(Color,"red");
最佳答案
使用函数模板和特化。在这些罕见情况下,我认为函数特化确实有帮助。
template<typename TEnum>
TEnum enum_cast(std::string const & s);
然后针对每个枚举类型专门化它。我改了名字,所以你可以像 Actor 一样阅读它:
Color color = enum_cast<Color>("blue");
或者parse
也是个好名字。选择你认为好的。我个人会选择 parse
或 enum_cast
。
现在对于 to_string
,您可以定义重载(您已经拥有):
std::string to_string(Color c);
std::string to_string(Race r);
std::string to_string(Direction d);
不要对 to_string
使用模板和特化。总的来说,重载确实很好。
关于c++ - 我可以实现其签名仅基于类型 ID 不同的模板化函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14321263/