私はこのコードがC++ 0xで合法であるかどうか不思議です。具体的には、関数move_it()
で宣言されたオブジェクトは、main()
で宣言されたオブジェクトに適切に移動されますか?このコードは合法ですか? (C++ 0xセマンティクスの移動)
#include <iostream>
#include <string>
#include <tr1/memory>
using namespace std;
class x
{
public:
x() { cout << "create " << this << endl; }
~x() { cout << "destroy " << this << endl; }
};
x&& move_it()
{
x r;
return move(r);
}
int main()
{
x n = move_it();
return 0;
}
コンパイルしてみましたか?エラーがありましたか?ここの問題は何ですか? – Neal
関数からローカルオブジェクトへの参照(任意の参照)を返す際に常にエラーが出るだけでなく、関数からの値の参照を返すことは意味がありません(static_castやstd :: moveの場合を除く)。 –