gpt4 book ai didi

c++ - 显式 bool 运算符 - 无法返回、测试、初始化 bool

转载 作者:行者123 更新时间:2023-11-28 00:34:53 33 4
gpt4 key购买 nike

我只是第一次尝试使用 explicit operator bool(),它的行为对我来说是相当出乎意料的。有人能解释一下为什么以下标有 //的部分不起作用

convertible 类将例如是一个智能指针类,能够检查所包含数据的有效性。

struct convertible
{
explicit operator bool() const
{
return ptr;
}

void* ptr = nullptr;
};

bool testReturn()
{
convertible test;
// does not work
return test;
}

bool testReturn2()
{
convertible test;
// does not work
return test == true;
}

bool testReturn3()
{
convertible test;
// works ?!
return !test;
}

int main()
{
convertible test;
// works
if (test) { }
// works
bool init(test);
bool tested = test;
bool tested2 = testReturn();
bool tested3 = testReturn2();
bool tested4 = testReturn3();
return 0;
}

使用 GCC 我得到:

milian@minime:/tmp$ g++ --version
g++ (GCC) 4.8.2 20131219 (prerelease)
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

milian@minime:/tmp$ g++ -std=c++11 test.cpp
test.cpp: In function ‘bool testReturn()’:
test.cpp:15:10: error: cannot convert ‘convertible’ to ‘bool’ in return
return test;
^
test.cpp: In function ‘bool testReturn2()’:
test.cpp:22:15: error: no match for ‘operator==’ (operand types are ‘convertible’ and ‘bool’)
return test == true;
^
test.cpp:22:15: note: candidate is:
test.cpp:22:15: note: operator==(int, int) <built-in>
test.cpp:22:15: note: no known conversion for argument 1 from ‘convertible’ to ‘int’
test.cpp: In function ‘int main()’:
test.cpp:39:17: error: cannot convert ‘convertible’ to ‘bool’ in initialization
bool tested = test;

与 Clang 类似:

milian@minime:/tmp$ clang++ --version
clang version 3.4 (tags/RELEASE_34/final)
Target: x86_64-unknown-linux-gnu
Thread model: posix
milian@minime:/tmp$ clang++ -std=c++11 test.cpp
test.cpp:15:10: error: no viable conversion from 'convertible' to 'bool'
return test;
^~~~
test.cpp:22:15: error: invalid operands to binary expression ('convertible' and 'int')
return test == true;
~~~~ ^ ~~~~
test.cpp:39:8: error: no viable conversion from 'convertible' to 'bool'
bool tested = test;
^ ~~~~
3 errors generated.

最佳答案

如果您的目标是禁用某些转换,只需通过delete 说明符禁用它们:

struct convertible
{
operator bool() const //(Implicit) Conversion to bool allowed
{
return true;
}

operator int() const = delete; //Implicit/Explicit conversions to int disallowed
//(Results in compilation error).
};


int main()
{
convertible c;

if( c )
...

int a = c; //ERROR
int b = (int)c; //ERROR (The same for static_cast<int>(c) )
}

您还可以删除所有类型,但使用转换运算符的模板化版本显式重载的类型除外:

struct foo
{
operator bool() const //Explicit overload for bool
{
return true;
}

template<typename T>
operator T() const = delete; //Everithing which is not a bool (Everithing which
//does not fit in the explicit overload) would
//resolve to this operator and will fail.
};


int main()
{
foo f;

bool b = f; //OK
int i = f; //ERROR
char c = f; //ERROR
etc...
}

关于c++ - 显式 bool 运算符 - 无法返回、测试、初始化 bool,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21350924/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com