c++ - 我可以实现其签名仅基于类型 ID 不同的模板化函数吗?

标签 c++ class function c++11 enums

这里有一些枚举类:

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也是个好名字。选择你认为好的。我个人会选择 parseenum_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/

相关文章:

c++ - 我应该使用基类还是子类来访问基类中定义的枚举值/常量?

c++ - 在 C++ 中,我们如何使用预先声明的类中的函数

javascript - Javascript 函数中的对象

jquery - 如果 jquery 中的类名更改,则删除函数

c++ - TCP 服务器不接受客户端发出的正确数量的连接,具有少量监听积压

c++ - 运算符重载时局部变量发出警告,按照教程,不知道该怎么办

c++ - 如何找到具有特定窗口样式的窗口? (WS_CHILDWINDOW)

c++ - 我们真的需要 C++11 中的 "enum class"吗?

c++ - 如何在类中存储常量数组?

c++ - 了解头文件