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};
                           ^~~~~
                           {    }

2 Answers 2

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?.

2
  • 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, 2017 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, 2017 at 4: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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.