c++ - 从 nullptr_t 到 bool : valid or not? 的转换

标签 c++ c++11 compiler-errors type-conversion language-lawyer

我用 3 个编译器测试了以下代码,得到了 3 个不同的结果:错误、警告和正常。

  • GCC (5.3):错误:从“std::nullptr_t”到“const Thing&”的无效用户定义转换
  • Clang (3.8):警告:将 nullptr 常量隐式转换为“bool”
  • MSVC (14.1):没有错误,没有警告

哪个编译器是正确的?我知道这是指针类型和 bool 之间的简单转换。但是 std::nullptr_tbool 是怎么回事?

(最后,Clang 和 MSVC 都可以处理代码。从积极的角度来看,Clang 稍微有点冗长。)

struct Thing
{
    Thing(bool) {}
};

void addThing(const Thing&){}

int main()
{
    addThing(nullptr); // warning or error on this line
}

最佳答案

这是无效的。根据boolean conversions的规则:

A prvalue of type std::nullptr_t, including nullptr, can be converted to a prvalue of type bool in context of direct-initialization. The resulting value is false.

标准引述,§7.14/1 Boolean conversions [conv.bool]

For direct-initialization, a prvalue of type std​::​nullptr_­t can be converted to a prvalue of type bool; the resulting value is false.

转换只允许direct-initialization , 但不是 copy-intialization ,其中包括按值将参数传递给函数的情况。例如

bool b1 = nullptr; // invalid
bool b2 {nullptr}; // valid

所以 GCC 是正确的。但是 Clang 也没有错;该标准只要求编译器在程序格式错误时“发出诊断”,因此它必须告诉您发生了什么事情,之后它可以做任何事情。

参见 Does the C++ standard specify that for some cases the compiling should fail with an error?

关于c++ - 从 nullptr_t 到 bool : valid or not? 的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43803368/

相关文章:

c++ - 对服务器/客户端应用程序使用 Kerberos 身份验证

c++ - 将 `std::vector` 替换为 `std::array`

c++ - 唯一的指针类初始化

android - Android Studio Gradle编译时间错误

c++ - 在成员初始化列表中填充 std::array

c++ - Union值的单函数比较

c++ - 错误 : cannot convert ‘int (^)(int)’ to ‘R (^)(T)’ in initialization

c++ - 抛出类似函数的可变参数宏包装,替换抛出的异常

c++ - CMake 编译时不包含链接库的路径

c - 奇怪的链接行为 - gcc 库