可能重复:
从函数返回 unique_ptr
20.7.1.2 [unique.ptr.single] 像这样定义复制构造函数:
// disable copy from lvalue
unique_ptr(const unique_ptr&) = delete;
unique_ptr& operator=(const unique_ptr&) = delete;
那么,为什么下面的代码编译得很好?
#include <memory>
#include <iostream>
std::unique_ptr< int > bar()
{
std::unique_ptr< int > p( new int(4));
return p;
}
int main()
{
auto p = bar();
std::cout<<*p<<std::endl;
}
我这样编译它:
g++ -O3 -Wall -Wextra -pedantic -std=c++0x kel.cpp
编译器:g++ 版本 4.6.1 20110908 (Red Hat 4.6.1-9)