Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++11 如何将bitmask_operators.hpp与命名空间和类一起使用_C++11_Templates_Bit Fields_Bitmask_Enum Class - Fatal编程技术网

C++11 如何将bitmask_operators.hpp与命名空间和类一起使用

C++11 如何将bitmask_operators.hpp与命名空间和类一起使用,c++11,templates,bit-fields,bitmask,enum-class,C++11,Templates,Bit Fields,Bitmask,Enum Class,我想使用C++11enum类作为位字段,并找到一种很好的方法 但如果我的枚举类声明不在全局名称空间中,而是在自定义名称空间中,或者在类内部,我就卡住了。例如: #define ENABLE_BIT_OPERATORS(E) template<> struct enable_bitmask_operators<E> { static constexpr bool enable=true; }; // anonymous namespace namespace { enum

我想使用C++11
enum类
作为位字段,并找到一种很好的方法

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

#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
然后我得到一个错误:

enclosing class of constexpr non-static member function 'bool MyclassGN::enable_bitmask_operators(MyclassGN::Ecgn)' is not a literal type
no match for 'operator|=' (operand types are 'MyclassGN::Ecgn' and 'MyclassGN::Ecgn')
我通过添加
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;
};
我有一个错误:

enclosing class of constexpr non-static member function 'bool MyclassGN::enable_bitmask_operators(MyclassGN::Ecgn)' is not a literal type
no match for 'operator|=' (operand types are 'MyclassGN::Ecgn' and 'MyclassGN::Ecgn')

更新了显示错误所在位置的示例。

基于Anthony Williams的
位掩码\u运算符的示例代码。hpp
还应用了Jay Millers的建议(constepr函数而不是模板结构)来修复命名空间问题

请注意,在类内声明enum类时,constexpr函数前面需要加上
friend
关键字(如下面的注释中dyp所建议的)。例如:

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

示例代码,基于Anthony Williams的bitmask_operators.hpp,还应用了Jay Millers的建议(constexpr函数而不是模板结构)来修复命名空间问题

请注意,在类内声明enum类时,constexpr函数前面需要加上
friend
关键字(如下面的注释中dyp所建议的)。例如:

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

“但是我没有按照他的提示去做”你能告诉我们你的尝试吗?“但是我没有按照他的提示去做”你能告诉我们你的尝试吗?你可以在类中使用一个friend函数(例如:),但不知道如何用一个宏来做,除了在命名空间范围声明该函数和使用限定ID引用枚举之外。但无论如何我都不喜欢宏,所以让
constexpr bool enable\u bitmask\u操作符
friend constexpr enable\u bitmask\u操作符
对我来说都很好。是的,使用
friend
关键字来完成这项工作。你可以在类内部使用friend函数(例如:),但不知道如何使用单个宏,除了在命名空间范围声明该函数和使用限定ID引用枚举之外。但无论如何,我不喜欢宏,所以让
constexpr bool enable\u bitmask\u操作符
friend constexpr enable\u bitmask\u操作符
对我来说都很好。是的,使用
friend
关键字来完成这项工作。