JTC1/SC22/WG14
N730
ISO/IEC JTC1/SC22/WG14 N730
Cleanup of aggregate initialization
Clive D.W. Feather
Abstract
========
The compound literals proposal - N716 - innocently introduces an
inconsistency in aggregate initialization. No-one has given a rationale
for it, and this paper proposes that it be removed.
Discussion
==========
Consider the following code:
struct s { int a; int b; };
int x = 1, y = 2;
struct s sx;
int main (void)
{
sx.a = 3; sx.b = 4;
{
int z = x; // Valid
struct s s1 = sx; // Valid
struct s s2 = (struct s) { x, y }; // Valid
struct s s3 = { x, y }; // Forbidden
/* ... */
}
}
There is no semantic difference between the declarations of s2 and s3,
yet the latter is forbidden by 6.5.7 paragraph 4:
All the expressions in an initializer for an object that has
static storage duration or in an initializer list for an object
that has aggregate or union type shall be constant expressions.
It has been suggested that the original constraint was to allow for
implementations where the dynamic initializer would have been a burden.
Since such implementations now have to cope with compound literals and
the declaration of s2, the restriction has outlived its usefulness.
Proposal
========
Change 6.5.7 paragraph 4 from:
All the expressions in an initializer for an object that has
static storage duration or in an initializer list for an object
that has aggregate or union type shall be constant expressions.
to:
All the expressions in an initializer for an object that has
| static storage duration shall be constant expressions.