假设我有一个unique_ptr
成员对象,我想在类中初始化,请参阅下面的代码。为什么我必须使用统一初始化(花括号)?第二个声明吐出一个错误,像独特的指针类初始化
so.cpp:10:31: error: expected parameter declarator
std::unique_ptr<Foo> upf2(new Foo);
^
so.cpp:10:31: error: expected ')'
so.cpp:10:30: note: to match this '('
std::unique_ptr<Foo> upf2(new Foo); ^
2 errors generated.
而且我不认为是一个最令人头疼的解析问题,至少我不相信如此。
#include <memory>
class Foo
{
};
class Bar{
std::unique_ptr<Foo> upf1{new Foo}; // works fine
// std::unique_ptr<Foo> upf2(new Foo); // error here
};
int main()
{
Bar bar;
}
NSDMIs必须使用* brace-equal-initializer *。 – 2014-10-09 02:37:24
@ T.C。谢谢,我不知道这是必须的。 – vsoftco 2014-10-09 02:40:16