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
.
[C++11: 5.19/3]:
A literal constant expression is a prvalue core constant expression of literal type, but not pointer type. An integral constant expression is a literal constant expression of integral or unscoped enumeration type. [..]
And:
[C++11: 3.9/10]:
A type is a literal type if it is:
- a scalar type; or
- a reference type; or
- a class type (Clause 9) that has all of the following properties: [..]
- an array of literal type.
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 compilation succeeds if I s/constexpr/const/
and compile with -ansi
rather than -std=c++11
.
The definition of integral constant expression explicitly allowed this case in C++03:
[C++03: 5.19/1]:
[..] An integral constant-expression can involve only literals (2.13), enumerators, const variables or static data members of integral or enumeration types initialized with constant expressions (8.5), non-type template parameters of integral or enumeration types, and sizeof
expressions. [..]