可能性の重複:
Returning unique_ptr from functionsなぜ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)
+1あなたが実際にどのコンパイラを使用しているのか、どのようにコードをコンパイルしたのかを実際に教えてください。 StackOverflow上のまれな光景です。 –
http://stackoverflow.com/questions/4316727/returning-unique-ptr-from-functionsを参照してください。 – Sjoerd