Assigned `nullptr` to` bool`. Which compiler is right?

I have the following code snippet that is assigned the type nullptrto bool.

#include <iostream>

int main()
{
    bool b = nullptr;
    std::cout << b;
}

In clang 3.8.0 it works fine. he gives way out 0. Clang demo

But g ++ 5.4.0 will throw an error:

source_file.cpp: In function ‘int main()’:
source_file.cpp:5:18: error: converting to ‘boolfrom ‘std::nullptr_t’ requires direct-initialization [-fpermissive]
         bool b = nullptr;

Which compiler is right?

+6
source share
1 answer

From the C ++ standard (4.12 Boolean conversions)

1 , , prvalue bool. , ; true. direct-initialization (8.5), prvalue std:: nullptr_t prvalue bool; false.

,

bool b( nullptr );

bool b = nullptr;

.

isocpp

+8

All Articles