C++ Logo

std-discussion

Advanced search

Is (0) a null pointer constant?

From: Lénárd Szolnoki <cpp_at_[hidden]>
Date: Fri, 21 Jul 2023 10:39:28 +0100
Hi,

Is a parenthesised integer literal with value zero a null pointer
constant?

The current wording doesn't seem to allow that. [conv.ptr] says that "a
null pointer constant is an integer literal ([lex.icon]) with value
zero ...", which should exclude (0).

All of gcc, clang and MSVC accept `void * ptr = (0);`.

According to [dcl.init] it's the initializer expression that is
converted to the destination type, but as the initializer expression is
`(0)`, which is not a null pointer constant, this should be ill-formed.
I found no wording that would suggest that conversion from the
subexpression `0` to `void *` should be considered.

MSVC also accepts `+0` and `-0` as null pointer constants, which is
much more clearly a bug (this is under /permissive-).

Cheers,
Lénárd

Received on 2023-07-21 09:39:34