50

I have this code:

#include <array>

int main(int, char **argv)
{
   std::array<int, 3> a = {1,2,3};
}

This compiles fine (-std=c++11) , but if you include -Wall it gives this warning that I don't understand:

clang_pp_error.cpp:5:28: warning: suggest braces around initialization of subobject [-Wmissing-braces]
   std::array<int, 3> a = {1,2,3};
                           ^~~~~
                           {    }
Scooter
  • 6,802
  • 8
  • 41
  • 64

2 Answers2

27

This should be a bug: https://llvm.org/bugs/show_bug.cgi?id=21629.

See also Is it wise to ignore gcc/clang's "-Wmissing-braces" warning?.

Community
  • 1
  • 1
FrankHB
  • 2,297
  • 23
  • 19
  • 7
    This warning should not be suppressed. If the code is according to C++11, initializer list using `{{ .. }}` is recommended instead of single `{ .. }`. There is a purpose for it and hence the warning should not be suppressed instead of improving the code that can cause problems later. – Sohail Si May 17 '17 at 17:12
  • 3
    @SohailSi This can be useful to prevent confusion between subobject to be initialized and the object being initialized, but it's plain wrong for cases like `std::array`, where the enclosing class is intended to be the replacement of the only array data member. Enforcement of this warning on such cases leaks the implementation details. Before it can be suppressed separately by somewhat portable means (e.g. attributes), it is not wise to be enabled by default. – FrankHB May 26 '17 at 04:24
24

Use std::array<int, 3> a = {{1,2,3}}; instead.

See Why wasn't a double curly braces syntax preferred for constructors taking a std::initializer_list

Community
  • 1
  • 1
Sohail Si
  • 2,750
  • 2
  • 22
  • 36