如何将bitmask_operators.hpp与名称空间和类一起使用



我想将C 11 enum class用作Bitfields,并在此处找到一个不错的方法。

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

#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

直到我将模板专业化放置在与bitmask_operators.hpp中的模板enable_bitmask_operators相同的名称空间(在这种情况下为全局名称空间)。

,但我想让我的专业知识接近我的枚举类声明。

在提到的文章中,杰伊·米勒(Jay Miller)也评论了这个问题,看来他提供了解决方案。但是我没有遵循他的提示来解决bitmask_operators.hpp

示例代码在这里。

编辑/部分求解:我同时,我得到了起作用(只是一个转储副本&amp; baste essea&copry essea&cryptic错误消息;-)。我刚刚通过应用Jay Millers Constexpr函数解决方案更新了我的示例代码。

但是,在班级内部声明枚举课时仍然存在问题。当我向我的班级添加CTOR时,问题就会加剧,例如:

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_) {}
};

但是,当我尝试使用bitmask运算符时,下一个问题会引发,例如:

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')

更新的示例显示了此错误。

示例代码,基于安东尼·威廉姆斯的bitmask_operators.hpp,还应用了Jay Millers建议(ConstexPr函数而不是模板&lt;> struct)来修复命名空间问题。

>

请注意,当在类中声明枚举类时,ConstexPR函数需要先于friend关键字(如下注释中所建议的DYP)。示例:

class Myclass {
public:
    enum class E {
        None = 0x00,
        Bit0 = 0x01,
        Bit1 = 0x02,
        Bit2 = 0x04,
        Bit3 = 0x08,
    };
    friend ENABLE_BIT_OPERATORS(E)
};

最新更新