Consider the following C++11 program, and its result in GCC 4.7.2:
int main()
{
constexpr int i = 0;
int* p = i;
}
// g++ -g -ggdb -Wall -Wextra -pedantic -std=c++11 t.cpp
// t.cpp: In function 'int main()':
// t.cpp:4:13: error: invalid conversion from 'int' to 'int*' [-fpermissive]
// t.cpp:4:9: warning: unused variable 'p' [-Wunused-variable]
According to the standard:
[C++11: 4.10/1]:
A null pointer constant is an integral constant expression (5.19) prvalue of integer type that evaluates to zero [..]
5.19 is a mess and I've failed to parse it fully, but wouldn't we expect i
to satisfy this criterion and act as a null pointer constant, consequently requiring no explicit conversion to int*
for the initialisation of p
?
The compilation succeeds if I s/constexpr/const/
and compile with -ansi
rather than -std=c++11
.
And:
At this point, I can't find a reason for that code to be non-compliant, so I suspect a GCC bug.
However it may be a deliberate bug given that the passage you quoted out of 4.10 is proposed to be changed (active issue #903) so that this would in fact be non-compliant code.
The definition of integral constant expression explicitly allowed this case in C++03: