[Bug c++/95330] Unneeded -Wmissing-braces warning when initialising std::array

redi at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Tue May 26 11:21:10 GMT 2020


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95330

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2020-05-26
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
           Keywords|                            |diagnostic

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Szőts Ákos from comment #0)
> According to cppreference.com [1] it's not necessary to use the second level
> of braces since CWG 1270 [2] from C++11 on.

CWG 1270 is not relevant here (you have misinterpreted what cppreference says).
As the DR says, brace elision before DR 1270 only applied in declarations of
the form T x = { a }, which is exactly the form you're using. So your code was
always valid, and was always accepted by GCC long before DR 1270.

So DR 1270 changes where brace elision is allowed, but doesn't affect your
example.

The GCC warning is telling you that you're relying on brace elision, which is
true. The extra braces aren't *required* but the warning doesn't say they're
required.

(In reply to Szőts Ákos from comment #0)
> The same message occurs when you initialise in either way:
>   * std::array<int, 3> a = {1, 2, 3};
>   * std::array<int, 3> a{1, 2, 3};

DR 1270 *does* apply to the second form (which is what cppreference says), it
makes it valid by allowing brace elision there. But that doesn't change that
you're relying on brace elision, which is what the warning tells you.

So the warning is still correct.

However, this is a recurring complaint from users and it's probably worth
making the diagnostic smarter. Clang has an "IdiomaticStdArrayInitDoesNotWarn"
test for this case:

https://github.com/llvm/llvm-project/blob/8aaabadeced32a1cd959a5b1524b9c927e82bcc0/clang/test/SemaCXX/aggregate-initialization.cpp#L155

It looks like they don't give a -Wmissing-braces warning when the number of
initializers is correct, but one level of braces around the entire list is
missing (which is the common case for std::array initialization). They still
warn if braces are elided from something like {{1, 2, 3}, 0}.

I think a reasonable heuristic is to suppress the warning if no initializers
occur outside the elided braces. So don't warn if {a,b,c} is used instead of
{{a,b,c}} but do warn if it's used instead of {{a,b},c} or {a,{b,c}}.

The text of the clang warning might also be less likely to confuse, as it says
"suggest braces around ..." rather than "missing braces around ...".


More information about the Gcc-bugs mailing list