小编csb*_*ako的帖子

如何为枚举创建模板运算符

我需要一个没有宏魔法的通用模板类,我可以像这样使用:

template<typename E>
class enum_operators
{
    E& operator++( E& orig )
    {
        orig = static_cast< E >( orig + 1 );
        return orig;
    }
};

enum colors : public enum_operators< colors >
{
    white,
    red,
    green,
    blue
};

enum corners : public enum_operators< corners >
{
    topleft,
    topright,
    bottomleft,
    bottomright
};
Run Code Online (Sandbox Code Playgroud)

是否可以使用可变参数模板或其他东西?我怎样才能做到这一点?

c++ templates enumeration enumerate c++11

4
推荐指数
1
解决办法
921
查看次数

标签 统计

c++ ×1

c++11 ×1

enumerate ×1

enumeration ×1

templates ×1