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 t
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:
const int x = 3;
.constexpr
: constexpr int x[] = {1,2,3};
.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.
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 is43
.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 ofx
.*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 expression42
.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.