来自 C++ 标准(4.12 布尔转换)
1 算术、无范围枚举、指针或指针的纯右值to 成员类型可以转换为 bool 类型的纯右值.一个零值、空指针值或空成员指针值被转换为假;任何其他值都将转换为 true.为直接初始化 (8.5),std::nullptr_t 类型的纯右值可以是转换为 bool 类型的纯右值;结果值为假.
1 A prvalue of arithmetic, unscoped enumeration, pointer, or pointer
to member type can be converted to a prvalue of type bool. A zero
value, null pointer value, or null member pointer value is converted
to false; any other value is converted to true. For
direct-initialization (8.5), a prvalue of type std::nullptr_t can be
converted to a prvalue of type bool; the resulting value is false.
所以这个声明
bool b( nullptr );
有效并且这个
bool b = nullptr;
错了.
我自己已经在 指出了这个问题isocpp
这篇关于将 `nullptr` 分配给 `bool` 类型.哪个编译器是正确的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持前端之家!
本文链接:https://www.f2er.com/3181260.html