编译下面的代码时,我在VC2010中遇到错误C2078.
struct A
{
int foo;
double bar;
};
std::array<A, 2> a1 =
// error C2078: too many initializers
{
{0, 0.1},
{2, 3.4}
};
// OK
std::array<double, 2> a2 = {0.1, 2.3};
Run Code Online (Sandbox Code Playgroud)
我发现正确的语法a1是
std::array<A, 2> a1 =
{{
{0, 0.1},
{2, 3.4}
}};
Run Code Online (Sandbox Code Playgroud)
问题是:为什么需要额外的括号a1但不是必需的a2?
更新
这个问题似乎并不特定于std :: array.一些例子:
struct B
{
int foo[2];
};
// OK
B meow1 = {1,2};
B bark1 = {{1,2}};
struct C
{
struct
{
int a, b;
} …Run Code Online (Sandbox Code Playgroud) 为什么这不能编译
#include <vector>
#include <array>
std::array<std::vector<const char*>, 2> s = {
{"abc", "def"},
{"ghi"}
};
Run Code Online (Sandbox Code Playgroud)
但这确实
#include <vector>
#include <array>
std::array<std::vector<const char*>, 2> s = {
std::vector{"abc", "def"},
{"ghi"}
};
Run Code Online (Sandbox Code Playgroud)
如果出于某种原因std::vector第一个需要,为什么第二个不需要呢?