のタイプ*this
は常に左辺値です。
§9.3.2 [class.this] p1
非静的(9.3)メンバー関数の本体では、キーワードthis
はprvalue式であり、その値は関数が呼び出されるオブジェクトのアドレスです。this
クラスのメンバー関数のタイプX
はですX*
。[...]
§5.3.1 [expr.unary.op] p1
単項演算*
子は間接参照を実行します。適用される式は、オブジェクト型へのポインタ、または関数型へのポインタであり、結果は、式が指すオブジェクトまたは関数を参照する左辺値になります。
std::move
したがって、moveコンストラクターを呼び出す場合はそうする必要があります。
次のコードスニペットは、次のことを示しています。
#include <iostream>
#include <utility>
struct test{
test(){}
test(test const&){ std::cout << "copy ctor // #1\n"; }
test(test&&){ std::cout << "move ctor // #2\n"; }
test f_no_move() &&{ return *this; }
test f_move() &&{ return std::move(*this); }
};
int main(){
test().f_no_move(); // #1
test().f_move(); // #2
}
Clang 3.1(ref-qualifiersを実装することがわかっている唯一のコンパイラー)を使用すると、次の出力が得られます。
$ clang ++ -std = c ++ 0x -stdlib = libc ++ -pedantic -Wall t.cpp
$ ./a.out
copy ctor //#1
move ctor //#2