Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/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 重载cast-to-bool可以将强制转换为int_C++11_Enums - Fatal编程技术网

C++11 重载cast-to-bool可以将强制转换为int

C++11 重载cast-to-bool可以将强制转换为int,c++11,enums,C++11,Enums,考虑以下用于包装枚举的类 class A { int v; A(int v) : v(v) { } public: enum B : int { X = 1, Y = 2, Z = 3 }; A(B v) : A(static_cast<int>(v)) { } A operator|(const A &b) const { return v | b.v; } operator bool() const { return v;

考虑以下用于包装枚举的类

class A {
    int v;
    A(int v) : v(v) { }
public:
    enum B : int { X = 1, Y = 2, Z = 3 };
    A(B v) : A(static_cast<int>(v)) { }
    A operator|(const A &b) const { return v | b.v; }
    operator bool() const { return v; }
};

int main() {
    A a(A::Y);
    if (a) 
        return 1;
    a = a | A::X; // this line failes to compile
    return 0;
}
有没有办法解决这一模棱两可的问题? bool运算符用于允许

if (a) ...
我宁愿坚持下去,也不愿使用类似的东西

if (!!a) ...

接线员

如果你让演员操作符显式呢?哇,这很有效,谢谢。介意发布一个答案,这样我就可以接受它吗?但为什么这在ifa中暗含着作用呢?在中找到了答案
if (a) ...
if (!!a) ...