[PATCH 2/2] Profile2: use an implicit initializer for a struct array
Lubomir I. Ivanov
neolit123 at gmail.com
Mon Mar 10 13:44:51 PDT 2014
On 10 March 2014 21:51, Thiago Macieira <thiago at macieira.org> wrote:
> Em seg 10 mar 2014, às 09:24:08, Dirk Hohndel escreveu:
>> I'll ask our resident compiler and language standards guru (Thiago) to
>> chime in here. I thought that the ISO standard requires the initializer
>> to be non-empty and that the '{}' initializer was a gcc-ism.
>
> I've just taken a quick look at the C99 standard. In 6.7.8 Initialization, the
> grammar does not allow empty braces:
>
> Syntax
> initializer:
> assignment-expression
> { initializer-list }
> { initializer-list , }
> initializer-list:
> designation_opt initializer
> initializer-list , designation_opt initializer
> designation:
> designator-list =
> designator-list:
> designator
> designator-list designator
> designator:
> [ constant-expression ]
> . identifier
>
> So if you open a brace, you must have at least one assignment-expression.
>
> [The C99 standard is not public, but the committee draft for C11 is:
> http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
> section 6.7.9, page 139]
>
>
> HOWEVER, this file is not C. It's C++ and C++11 does allow for {}. See 8.5
> Initializers [dcl.init], with the syntax:
>
> initializer:
> brace-or-equal-initializer
> ( expression-list )
> brace-or-equal-initializer:
> = initializer-clause
> braced-init-list
> braced-init-list:
> { initializer-list ,opt }
> {}
>
> So the {} is explicitly allowed in C++11.
>
> [The C++11 final draft is not publicly available, but the editor revised
> version is: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf,
> search for [dcl.init]; or see
> https://github.com/cplusplus/draft/blob/master/source/declarators.tex#L2372]
i think this is also allowed in c++98, but i'm not exactly certain if
to trust gcc:
----------
g++ test.cpp -std=c++98 -pedantic -Wextra
struct {
int i;
double d;
} a[2] = {};
----------
c++98 has this in 8.5.1.8:
"...An empty initializer-list can be used to initialize any aggregate.
If the aggregate is not an
empty class, then each member of the aggregate shall be initialized
with a value of the form T()(5.2.3),
where T represents the type of the uninitialized member."
lubomir
--
More information about the subsurface
mailing list