On 31/12/2024 12:27, Daria via Gcc-help wrote:
Given the following code:
int main() {
int constexpr foo[1] = { 4 };
static int i = foo[0];
static int j = *foo;
}
gcc14 -std=c23 gives:
x.c: In function 'main':
x.c:4:20: error: initializer element is not constant
4 | static int j = *foo;
| ^
Why is the line above okay, but this one is an error?
C23 6.5.3.2p2 says "The definition of the subscript operator [] is that E1[E2] is identical
to (*((E1)+(E2)))."
This would suggest that foo[0] is identical to *(foo+0) is identical to *foo?
Looking at section 6.6p12:
"""
The array-subscript [] and member-access -> operator, the address & and
indirection * unary operators, and pointer casts can be used in the
creation of an address constant, but the value of an object shall not be
accessed by use of these operators.
"""
As I see it, neither of "i" or "j" are required to be accepted as
constant expressions. The standard allows (in p14) an implementation to
accept additional forms as "extended constant expressions". So it seems
to me that "foo[0]" is a form accepted by gcc as an extension, while
"*foo" is not accepted.
Maybe it would be better if gcc /did/ accept it - gcc developers will
have to answer that one.
(clang accepts both "foo[0]" and "*foo" as initialisers here.)
David