【发布时间】:2015-03-04 11:29:57
【问题描述】:
谁能告诉我,unique_ptr 的以下初始化有什么问题?
int main()
{
unique_ptr<int> py(nullptr);
py = new int;
....
}
g++ -O2 xxx.cc -lm -o xxx -std=c++11 说:
error: no match for ‘operator=’ (operand types are ‘std::unique_ptr<int>’ and ‘int*’)
py = new int;
^
在做
unique_ptr<int> px(new int);
工作得很好。
【问题讨论】:
-
试试
py.reset(new int) -
或
py = std::make_unique<int>();
标签: c++ c++11 initialization unique-ptr