3

I've read some other posts about -Wmissing-braces:

The answers to that last question make it seem like array<int, 3> x = {1,2,3} should work fine. cppreference also says:

As an aggregate type, it can be initialized with aggregate-initialization given at most N initializers that are convertible to T: std::array<int, 3> a = {1,2,3};.

However, it appears Clang still issues a warning in all of these cases:

#include <vector>
#include <array>
int main() {
    std::vector<int> v1({1, 2, 3});
    std::vector<int> v2{1, 2, 3};
    std::vector<int> v3 = {1, 2, 3};

    std::array<int, 3> a1({1, 2, 3});  // warning
    std::array<int, 3> a2{1, 2, 3};    // warning
    std::array<int, 3> a3 = {1, 2, 3}; // warning - at least this should be allowed

    std::vector<std::vector<int>> vs;
    vs.push_back({1, 2, 3});

    std::vector<std::array<int, 3>> as;
    as.push_back({1, 2, 3});           // warning

    return 0;
}

source_file.cpp:11:25: warning: suggest braces around initialization of subobject [-Wmissing-braces]
        std::array<int, 3> a1({1, 2, 3});
                               ^~~~~~~
                               {      }
source_file.cpp:12:24: warning: suggest braces around initialization of subobject [-Wmissing-braces]
        std::array<int, 3> a2{1, 2, 3};
                              ^~~~~~~
                              {      }
source_file.cpp:13:27: warning: suggest braces around initialization of subobject [-Wmissing-braces]
        std::array<int, 3> a3 = {1, 2, 3};
                                 ^~~~~~~
                                 {      }
source_file.cpp:19:16: warning: suggest braces around initialization of subobject [-Wmissing-braces]
        as.push_back({1, 2, 3});
                      ^~~~~~~
                      {      }

Why do all of these produce warnings? Are some of them technically incorrect?

Community
  • 1
  • 1
jtbandes
  • 115,675
  • 35
  • 233
  • 266
  • 2
    The rules have changed during standardization, and the compiler might not be sure that it and you agree on the rules for brace elision. If you add another set of braces, it works regardless (but looks uglier). – Bo Persson Jan 02 '17 at 19:26
  • 3
    Don't use `-Weverything` if you don't know what it's for. It enables *all possible* warnings, *not* all useful warnings. That said, with clang (unlike gcc), the warning is still emitted with `-Wall` in current versions as well, and `-Wall` is supposed to be limited to useful warnings. –  Jan 02 '17 at 19:44
  • I'm aware that it enables all possible warnings and that's why I like using it. :-) Every warning was created for a reason, even if it may not be considered "useful" by everyone. – jtbandes Jan 02 '17 at 19:52
  • Looks like clang didn't implement [dr 1270](http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1270) yet. It's status is Unknown, according to [C++ Defect Report Support in Clang](http://clang.llvm.org/cxx_dr_status.html). – ks1322 Jan 02 '17 at 20:39
  • @ks1322 The fact that the OP's code compiles means that it is implemented. Without it, `std::array a2{1, 2, 3};` would just be an error. The warning is specifically meant to apply to valid code, and `std::array a3 = {1, 2, 3};` was already explicitly valid before that DR. –  Jan 02 '17 at 21:08
  • Is there a way to initialize such an array without triggering such a warning? – PiRK Feb 17 '22 at 09:54

0 Answers0