0

我想使用 C++11作为位域并在这里enum class找到一个不错的方法。

但是我坚持,如果我的枚举类声明不在全局命名空间中,而是在自定义命名空间中或在类内部。例如:

#define ENABLE_BIT_OPERATORS(E) template<> struct enable_bitmask_operators<E> { static constexpr bool enable=true; };

// anonymous namespace
namespace {
enum class Ean {
    None = 0x00,
    Bit0 = 0x01,
    Bit1 = 0x02,
    Bit2 = 0x04,
    Bit3 = 0x08,
};
ENABLE_BIT_OPERATORS(Ean)
} // anonymous namespace

// custom namespace
namespace Custom {
enum class Ecn {
    None = 0x00,
    Bit0 = 0x01,
    Bit1 = 0x02,
    Bit2 = 0x04,
    Bit3 = 0x08,
};
ENABLE_BIT_OPERATORS(Ecn)
} // custom namespace

// inside class in global namespace
class MyclassGN {
public:
    enum class Ecgn {
        None = 0x00,
        Bit0 = 0x01,
        Bit1 = 0x02,
        Bit2 = 0x04,
        Bit3 = 0x08,
    };
    ENABLE_BIT_OPERATORS(Ecgn)
};

// inside class in anonymous namespace
namespace {
class MyclassAN {
public:
    enum class Ecan {
        None = 0x00,
        Bit0 = 0x01,
        Bit1 = 0x02,
        Bit2 = 0x04,
        Bit3 = 0x08,
    };
    ENABLE_BIT_OPERATORS(Ecan)
};
} // anonymous namespace

// inside class in custom namespace
namespace Custom {
class MyclassGN {
public:
    enum class Ecgn {
        None = 0x00,
        Bit0 = 0x01,
        Bit1 = 0x02,
        Bit2 = 0x04,
        Bit3 = 0x08,
    };
    ENABLE_BIT_OPERATORS(Ecgn)
};
} // custom namespace

这总是给我错误:

error: specialization of 'template<class E> struct enable_bitmask_operators' in different namespace

直到我将模板特化放置到与所在模板相同的命名空间(在本例enable_bitmask_operatorsbitmask_operators.hpp为全局命名空间)。

但我想让我的专长接近我的枚举类声明。

在提到的文章中,Jay Miller 也评论了这个问题,似乎他提供了一个解决方案。但我没有按照他的提示解决这个问题bitmask_operators.hpp

示例代码在这里

编辑/部分解决:我同时,我得到了它的工作(只是一个转储复制和粘贴问题和神秘的错误消息;-)。我刚刚通过应用 Jay Millers constexpr 函数解决方案更新了我的示例代码。

但是在类中声明枚举类时仍然存在问题。当我向我的班级添加一个演员时,这个问题就出现了,比如:

class MyclassGN {
public:
    enum class Ecgn {
        None = 0x00,
        Bit0 = 0x01,
        Bit1 = 0x02,
        Bit2 = 0x04,
        Bit3 = 0x08,
    };
    ENABLE_BIT_OPERATORS(Ecgn)
    explicit MyclassGN(Ecgn e_) {}
};

然后我得到一个错误:

enclosing class of constexpr non-static member function 'bool MyclassGN::enable_bitmask_operators(MyclassGN::Ecgn)' is not a literal type

好吧,我通过添加static关键字来解决这个问题:

class MyclassGN {
public:
    enum class Ecgn {
        None = 0x00,
        Bit0 = 0x01,
        Bit1 = 0x02,
        Bit2 = 0x04,
        Bit3 = 0x08,
    };
    static ENABLE_BIT_OPERATORS(Ecgn)
    explicit MyclassGN(Ecgn e_) {}
};

但是当我尝试使用位掩码运算符时会出现下一个问题,例如:

class MyclassGN {
public:
    enum class Ecgn {
        None = 0x00,
        Bit0 = 0x01,
        Bit1 = 0x02,
        Bit2 = 0x04,
        Bit3 = 0x08,
    };
    static ENABLE_BIT_OPERATORS(Ecgn)
    explicit MyclassGN(Ecgn e_): e(e_) {
        e |= Ecgn::Bit3;
    }
private:
    Ecgn e;
};

我收到一个错误:

no match for 'operator|=' (operand types are 'MyclassGN::Ecgn' and 'MyclassGN::Ecgn')

显示错误的更新示例位于此处

4

1 回答 1

2

示例代码,基于 Anthony Williamsbitmask_operators.hpp并应用 Jay Millers 建议(constexpr 函数而不是 template<> 结构)来修复命名空间问题,位于此处

请注意,在类中声明枚举类时, constexpr 函数需要在friend关键字前面(如 dyp 在下面的评论中建议的那样)。例子:

class Myclass {
public:
    enum class E {
        None = 0x00,
        Bit0 = 0x01,
        Bit1 = 0x02,
        Bit2 = 0x04,
        Bit3 = 0x08,
    };
    friend ENABLE_BIT_OPERATORS(E)
};
于 2017-02-27T12:11:47.423 回答