Is this constexpr integer not a null pointer constant?

后端 未结 1 1711
我寻月下人不归
我寻月下人不归 2021-01-12 06:49

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 -peda         


        
相关标签:
1条回答
  • 2021-01-12 07:13

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

    0 讨论(0)
提交回复
热议问题