8

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.

5
  • I wouldn't expect it to be a null pointer constant, mainly because i is a lvalue. However, since the expression somewhat involves lvalue-to-rvalue conversions and after that, the standard makes a constant expression, we can reasonably argue it is a NPC however perverse that might be. Note that it causes chaos in overload resolution, esp. when you're using unknown values eg. in templates. BTW I wouldn't take gcc as an benchmark here, their handling of constants is horrible. Commented Nov 21, 2012 at 13:02
  • 3
    If the language were redesigned from scratch today than nullptr would be required everywhere and you would never be able to assign an integral type (constant or otherwise) to a pointer without a cast. I think it makes sense to limit it only to a literal 0 for backwards compatibility, there is no use for using a more complicated constant expression that happens to evaluate to zero as a null pointer constant. Commented Nov 21, 2012 at 13:03
  • 2
    Please note Lippman's C++ primer, 5th edition (covering c++11) Exercise 2.32 which is pretty much the same thing. I'm thinking that the OP has the correct answer, and gcc is what's giving the issue here. Any comments? Commented Feb 22, 2015 at 21:11
  • @FlipMcF: At time of writing, the committee has this issue as "pending review" still. Commented Feb 23, 2015 at 1:12
  • 1
    Came here bewildered while solving the mentioned ex 2.32. Commented Jun 22, 2021 at 23:37

1 Answer 1

8

[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. [..]

Sign up to request clarification or add additional context in comments.

7 Comments

Actually, your emphasis is wrong. You should have emphasized literal constant expression, and int is a literal type.
I think you are right that this is a GCC bug. However, that is only because this is actually a defect in the current standard. It is planned to remove this thing in the next iteration and only allow actual literal zeros as null pointer constants (i.e. none of that nonsense like 1-1 and so on)
@R.MartinhoFernandes: Ah, interesting! Do you have an issue number perchance?
For reference, this is the related DR.
weird how you say "you" to yourself.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.