I've read some other posts about -Wmissing-braces
:
- Is it wise to ignore gcc/clang's "-Wmissing-braces" warning?
- What's the correct way to initialize a member array with an initializer list?
- Brace elision in std::array initialization
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 toT
: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