【发布时间】:2019-01-10 19:18:46
【问题描述】:
我的理解是,nullptr 无法隐式转换为其他类型。但后来我“发现”它可以转换为布尔值。 问题是,我可以看到它在 GCC 4.x 上被转换为 bool,但它在 GCC > 5.X 上抱怨
#include <iostream>
bool f(bool a){
return !a;
}
// Type your code here, or load an example.
int main() {
return f(nullptr);
}
在 >5.x 我得到
<source>: In function 'int main()':
<source>:7:21: error: converting to 'bool' from 'std::nullptr_t' requires direct-initialization [-fpermissive]
return f(nullptr);
^
<source>:2:6: note: initializing argument 1 of 'bool f(bool)'
bool f(bool a){
^
Compiler returned: 1
我在 GCC 5.X 的发行说明中找不到任何可以解释这一点的内容。
可以在这里观察到: https://godbolt.org/g/1Uc2nM
有人可以解释为什么版本之间存在差异以及这里应用了什么规则。
【问题讨论】: