Consider the following program:
#include <array>
int main()
{
std::array<int, 1> x = { 0 }; // warning!
x = { { 0 } }; // no warning
return 0;
}
The first initialization leads to warnings on gcc 4.7.2...
main.cpp:5:22: warning: unused variable ‘x’ [-Wunused-variable]
... and clang 3.1
main.cpp:5:28: warning: suggest braces around initialization of subobject [-Wmissing-braces]
std::array<int, 1> x = { 0 };
As far as the standard goes, there should be no difference between double or single curly braces, at least in this example.
There are two ways to deal with the warning:
- Just turn it off
- Fix the code, so the compiler is happy
What do you propose? IMHO, the double curly expression looks somewhat ugly. On the other hand, the warning might detect real problems in more complicated examples. Do you know an example where the warning would have helped you?
Clang 6.0 suppresses the warning about missing braces. The svn log says:
So I would omit the braces and disable
-Wmissing-braces
for Clang prior to 6.0 if it needs to be supported.I get the same warning in Xcode 6.1.1 (the current version as of March 9, 2015). When I add the extra braces around each subobject I get an error. When I add an extra set of braces around the entire initialization list then the warning goes away. According to the standard specification 14882:2011 23.3.2.1 [array.overview] subsection 2 explicitly states
result of code in Xcode 6.1.1 (below)
When we look at 14882:2011 8.5 [dcl.init] subsection 1 we see that an 'initializer-list' can optionally contain an 'initializer-clause', which itself can be a 'braced-init-list'. So either way should be correct. Though based on the spec I personally think single braces shouldn't output a compiler warning for a std::array initializer-list, and double braces is overkill.
When ignoring the Clang warning with
-Wno-missing-braces
, I would recommend to enable-Wmissing-field-initializers
(or use-Wextra
, which also includes it). Otherwise, you miss a useful warning like in this example:For comparison, this is what GCC does:
In summary:
-Wno-missing-braces -Wmissing-field-initializers
to silence the warning without loosing other useful warningsstd::array<int, 1> x = { 0 };
example, so there is no need to disable any warnings. However, I would recommend to enable-Wmissing-field-initializers
(or use-Wextra
), as it is not enabled by-Wall
.-Wmissing-braces
will no longer be enabled in GCC's-Wall
(for C++ mode), as of 4.8, for precisely the reason you describe. For current versions of GCC, either disable or ignore the warning, the code you have is written the way it should be.The warning is probably meant to cover code such as
However, IMHO, that is already handled well enough by
-Wmissing-field-initializers
, which does not warn about your original code.