Assigned `nullptr` to `bool` type. Which compiler is correct?

前端 未结 2 1126
野的像风
野的像风 2021-01-12 12:05

I have a following snippet of code that assigned nullptr to bool type.

#include 

int main()
{
    bool b = nullptr         


        
2条回答
  •  离开以前
    2021-01-12 12:22

    From the C++ Standard (4.12 Boolean conversions)

    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.

    So this declaration

    bool b( nullptr );
    

    is valid and this

    bool b = nullptr;
    

    is wrong.

    I myself pointed out already this problem at isocpp

提交回复
热议问题