lundi 2 janvier 2017

Why does Clang warn about missing braces for std::array?

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?

Aucun commentaire:

Enregistrer un commentaire