x[0] == 1 constant expression in C++11 when x is const int[]?

十年热恋 提交于 2019-11-29 06:04:46

问题


Is the following C++11 program ill-formed?

const int x[] = {1,2,3};

static_assert(x[0] == 1, "yay");

int main() {}

gcc and clang seem to think so, but why isn't x[0] == 1 a constant expression?

x[0] == 1
subscript operator
*(x+0) == 1
array-to-pointer conversion (int* p = x)
*(p+0) == 1
pointer addition
*p == 1
indirection (lvalue y = x[0])
y == 1
lvalue-to-rvalue conversion:

a non-volatile glvalue (yes, x[0] is a glvalue and non-volatile) of integral (yes it has type const int) or enumeration type that refers to a non-volatile const object (yes it has type const int) with a preceding initialization (yes initialized with 1), initialized with a constant expression (yes 1 is constant expression)

Seems true, the first element of the x array satisfies these conditions.

1 == 1

?

Is this a compiler bug, standard defect, or am i missing something?

What part of 5.19 [expr.const] says this isn't a constant expression?


回答1:


In 5.19:

A [...]expression is a constant expression unless it involves one of the following [...]:

  • an lvalue-to-rvalue conversion (4.1) unless it is applied to

    • a glvalue of integral or enumeration type that refers to a non-volatile const object with a preceding initialization, initialized with a constant expression, or
    • a glvalue of literal type that refers to a non-volatile object defined with constexpr, or that refers to a sub-object of such an object, or
    • a glvalue of literal type that refers to a non-volatile temporary object initialized with a constant expression

Putting it plainly, lvalue-to-rvalue conversion can only be done in constant expressions if:

  • a constant integral (or enum) declaration initialized with a constant: const int x = 3;.
  • a declaration with constexpr: constexpr int x[] = {1,2,3};.
  • a temporary object initialized with a constant expression...

Your example does include lvalue-to-rvalue conversion, but has none of these exceptions, so x is not a constant expression. If, however, you change it to:

constexpr int x[] = {1,2,3};

static_assert(x[0] == 1, "yay");

int main() {}

Then all is well.




回答2:


By the current wording of the standard it is a compiler bug, and the program is well-formed. It is now being considered whether it should be a standard defect, as it would be difficult to implement.

For a detailed explanation see:

https://groups.google.com/a/isocpp.org/forum/?fromgroups#!topic/std-discussion/Nv5_2dCHm6M

Report copied below:

The current wording of the C++11 official through to N3690 inclusive has the following:

A conditional-expression e is a core constant expression unless the evaluation of e would evaluate one of the following expressions:

  • an lvalue-to-rvalue conversion (4.1) unless it is applied to
    • a non-volatile glvalue of integral or enumeration type that refers to a non-volatile const object with a preceding initialization, initialized with a constant expression

The following declaration at global scope:

const int x[2] = {42, 43};

defines an array of 2 const int objects, list-initialized with {42, 43}

In 8.5.1 [dcl.init.aggr]/2:

When an aggregate is initialized by an initializer list, as specified in 8.5.4, the elements of the initializer list are taken as initializers for the members of the aggregate, in increasing subscript or member order.

So the initializer of the first element object is 42 and the initializer of the second element object is 43.

The expression *x is an lvalue and a core constant expression. It entails an array-to-pointer conversion, and an indirection - neither of which disqualify the expression as a core constant expression. The glvalue expression refers to the first element object of x. *x is a non-volatile glvalue of integral type (const int) that refers to a non-volatile const object with a preceding initialization, and intialized with the constant expression 42.

Therefore an lvalue-to-rvalue conversion applied to the glvalue *x is allowed in a constant expression, and so the following is well-formed:

constexpr int y = *x;

Neither gcc or clang trunk currently accept this as a constant expression, despite it being well-formed according to the standard.

Is this intended?

A full demo program:

const int x[2] = {42, 43};
constexpr int y = *x;
int main() {}

Implementations similarly fail with the equivalent lvalue x[0] as well.



来源:https://stackoverflow.com/questions/18903113/x0-1-constant-expression-in-c11-when-x-is-const-int

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!