I tested the following code with 3 compilers and got 3 different results: error, warning and ok.
Which compiler is correct? I know it's a trivial conversion between a pointer type and bool
. But what's about std::nullptr_t
and bool
?
(In the end, Clang and MSVC are both fine with the code. Clang is a little bit more verbose in a positive way.)
struct Thing
{
Thing(bool) {}
};
void addThing(const Thing&){}
int main()
{
addThing(nullptr); // warning or error on this line
}
This is invalid. According to the rule of boolean conversions:
A prvalue of type
std::nullptr_t
, includingnullptr
, can be converted to a prvalue of typebool
in context of direct-initialization. The resulting value isfalse
.
Quotes from the standard, §7.14/1 Boolean conversions [conv.bool]
For direct-initialization, a prvalue of type
std::nullptr_t
can be converted to a prvalue of typebool
; the resulting value isfalse
.
The conversion is only allowed for direct-initialization, but not copy-intialization, which including the case for passing argument to a function by value. e.g.
bool b1 = nullptr; // invalid
bool b2 {nullptr}; // valid
So GCC is correct. But Clang is not wrong either; the standard only requires the compiler to "issue a diagnostic" when program is ill-formed, so it must tell you something happens, after that it could do anything.
See Does the C++ standard specify that for some cases the compiling should fail with an error?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With