#include <memory>
struct foo
{
std::unique_ptr<int> p;
};
int main()
{
foo bar { std::unique_ptr<int>(new int(42)) };
// okay
new foo { std::unique_ptr<int>(new int(42)) };
// error: no matching function for call to
// 'foo::foo(<brace-enclosed initializer list>)'
}
动态对象是否不支持统一初始化,或者这是g++ 4.6.1的缺陷?
它可以在g++ 4.7.1中工作,但是如果foo继承另一个类,则main中的两行都无法编译:
struct baz
{
// no data members, just some member functions
};
struct foo : baz
{
std::unique_ptr<int> p;
};
再次出现编译器的缺陷?还是统一初始化与继承不兼容?
new foo({std::unique_ptr<int>(new int(42))});
这个在4.6.1版本中可以正常工作。 - jrok