为什么函数向bool存在隐式转换
Why is there Implicit conversion of function to bool?
我试图在我的代码中找到一个错误,发现返回函数名称被隐式转换为bool为true:
bool isArabicNumeral(char arg) { /* Details not important*/ };
bool isValidVarNameFirstChar(char arg) { /* Details not important */ }
bool isValidVarNameContinuationChar(char arg)
{
return isValidVarNameFirstChar || isArabicNumeral(arg) ? true : false;
// I forgot to write the brackets after isValidVarNameFirstChar
// so the function wasn't called. This always returns true.
}
所以我发现C 允许转换在任何地方都可以进行这种转换,因此我可以这样做:
void afunction() {};
int main()
{
bool boolVariable = afunction; // This works
int intVariable = afunction; // This doesn't work
return 0;
}
我的问题是,我知道从函数到布尔有一个隐式转换,但是为什么它总是返回的。另外,实际上是在转换为布尔?它是函数指针,因此实际上它是返回类似0xF7B3A1D0的内容,并且由于非零的内容是正确的吗?还是返回函数TypeName?或者是其他东西?这很奇怪,通常,当我在函数之后不小心离开括号时,我的编译器通常会说"非标准使用功能调用"。
另外,这是如何有用的转换?
是函数指针,因此实际上它是返回诸如0xf7b3a1d0之类的东西,然后转换为true,因为非零的内容是true的?
是。
是的,原因是bool检查只是查看是否不是false(又称0),如果不是false,则是真的。
相关文章:
- 显式 std::exception_ptr 转换为 bool 不存在.VS2010 错误?
- 如果存在从"双精度"到"T"的转换,则禁用构造函数
- 将文件复制到自定义位置,存在字符串转换问题
- 为什么我的编译器无法弄清楚这种转换,它何时存在?
- 将字符串转换为整数类型T,检查是否存在溢出
- 指向类成员函数的指针中存在类型转换错误
- 不存在从"Magick::Color"到"MagickCore::Quantum"的合适转换功能
- C++不存在合适的转换函数
- 是否存在将长整型转换为指针有效的情况
- 转换构造函数和运算符都存在且涉及显式性时的行为
- 不存在从 "vector<string>" 到 "vector<string>" * 的合适转换?
- 句子转换器在存在新行的情况下不起作用
- 不存在从"const std::string"到"time_t"的合适转换功能
- 我收到错误:没有合适的转换函数从 std::basic_istream<char、std::char_traits<char>> 到 char 存在
- Clang和GCC在转换C++17中非类型模板参数的自动说明符中存在分歧
- C++枚举类:强制转换为不存在的条目
- 放置 new 的返回值与其操作数的强制转换值之间是否存在(语义)差异
- 在将平截头体从摄影机空间转换为灯光空间以进行阴影映射方面存在主要问题
- 为什么转换运算符在存在 const-ref 和值时会导致不明确的重载
- 从派生 * 到基 * 的转换存在,但无法访问