std::unique_ptr<int> ptr() {
std::unique_ptr<int> p(new int(3));
return p; // Why doesn't this require explicit move using std::move?
} // Why didn't the data pointed to by 'p' is not destroyed here though p is not moved?
int main() {
std::unique_ptr<int> a = ptr(); // Why doesn't this require std::move?
std::cout << *a; // Prints 3.
}
在上面的代码中,该函数ptr()
返回一个p
. 当p
超出范围时,数据“3”应该被删除。但是代码如何在没有任何访问冲突的情况下工作?