让我们考虑这段代码:
#include <vector>
#include <array>
int main()
{
std::vector<std::array<int, 3>> arr;
arr.push_back({ 1,2,3}); // WARNING
arr.push_back({{4,5,6}}); // All good
std::array<int, 3> a1 {1,1,1}; // WARNING
std::array<int, 3> a2 {{2,2,2}}; // All good
std::vector<int> a3 {3,3,3}; // All good
std::vector<int> a4 {{4,4,4}}; // All good
return 0;
}
一切都正确编译,所有向量和数组都包含预期的元素。
但是,当在 gcc 中使用标志-Wmissing-braces
(用 10.2 测试)时,我们得到:
<source>:8:27: warning: missing braces around initializer for 'std::__array_traits<int, 3>::_Type' {aka 'int [3]'} [-Wmissing-braces]
8 | arr.push_back({ 1,2,3}); // WARNING
11 | std::array<int, 3> a1 {1,1,1}; // WARNING
为什么 gcc 显示此警告?这是按照https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53119或https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80454中建议的错误如何修复警告:初始化器周围缺少大括号?,或者上面的两条标记线真的有问题吗?