C++ 在非pod结构上使用运算符new+;初始化成员列表

C++ 在非pod结构上使用运算符new+;初始化成员列表,c++,c++11,new-operator,initializer-list,C++,C++11,New Operator,Initializer List,我试着在堆上分配包含非pod成员的结构,并使用初始化列表初始化它们。但是,编译器在我的代码中遇到错误。此代码段复制了它: #include <vector> struct A { int a; }; struct B { int a; std::vector<int> b; }; int main() { std::vector<int> some_vec; A a = {1}; // OK A b = A

我试着在堆上分配包含非pod成员的结构,并使用初始化列表初始化它们。但是,编译器在我的代码中遇到错误。此代码段复制了它:

#include <vector>

struct A {
    int a;
};

struct B {
    int a;
    std::vector<int> b;
};

int main() {
    std::vector<int> some_vec;
    A a = {1}; // OK
    A b = A{1}; // OK
    A *c = new A{1}; // OK(leaks, NP)
    B d = {1, some_vec}; // OK
    B e = B{1, some_vec}; // OK

    B *f = new B{1, some_vec}; // Fails to compile

    B *g = new B({1, some_vec}); // OK
}
#包括
结构A{
INTA;
};
结构B{
INTA;
std::载体b;
};
int main(){
std::vector some_vec;
A={1};//好的
A b=A{1};//好的
A*c=newa{1};//正常(泄漏,NP)
bd={1,some_vec};//好的
be=B{1,some_vec};//好的
B*f=newb{1,some_vec};//编译失败
B*g=newb({1,some_vec});//好的
}
(我知道有漏洞,我知道,这只是一个测试片段)

指出的行未能在GCC 4.6.3中编译,出现以下错误:

test.cpp: In function ‘int main()’:
test.cpp:19:29: error: no matching function for call to ‘B::B(<brace-enclosed initializer list>)’
test.cpp:19:29: note: candidates are:
test.cpp:7:8: note: B::B()
test.cpp:7:8: note:   candidate expects 0 arguments, 2 provided
test.cpp:7:8: note: B::B(const B&)
test.cpp:7:8: note:   candidate expects 1 argument, 2 provided
test.cpp:7:8: note: B::B(B&&)
test.cpp:7:8: note:   candidate expects 1 argument, 2 provided
test.cpp:在函数“int main()”中:
test.cpp:19:29:错误:调用“B::B()”时没有匹配的函数
考试。cpp:19:29:注:考生为:
test.cpp:7:8:note:B::B()
test.cpp:7:8:注意:候选者需要0个参数,提供2个
test.cpp:7:8:note:B::B(常量B&)
test.cpp:7:8:注意:候选者期望1个参数,提供2个参数
测试cpp:7:8:注:B::B(B&&)
test.cpp:7:8:注意:候选者期望1个参数,提供2个参数
显然,编译器无法使用提供的初始值设定项列表初始化我的结构。奇怪的是,在产生错误的那一行之后的下一行(据我所知)只是从使用相同初始值设定项列表构造的另一行复制(可能移动)a
B
,没有产生任何错误


我做错什么了吗?我的意思是,我可以使用提供的代码段上的最后一行,但是有什么原因不能使用操作符new和初始化列表创建一个结构吗?

代码应该编译并工作。我用GCC4.7.0对它进行了测试,它工作得很好,所以这个bug似乎已经修复了。如果您阅读标准中的8.5.4列表初始化,他们会说
列表初始化可以用作新表达式(5.3.4)中的初始值设定项

我怀疑这可能是GCC中的错误。谢谢你的尝试。