0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

std::move(T&&) しただけではムーブコンストラクタもムーブオペレータも呼び出しされない

Last updated at Posted at 2020-10-11

c++でムーブコンストラクタを特定の条件下で呼び出したいことがあり、std::move(T&&)を呼び出すコードを書いた。

しかし、デバッグしてみると対象のムーブコンストラクタが呼び出しされなかった。

暫く悩んだのですが、
この時書いたコードがstd::move(obj)というコードであり、Obj obj = std::move(obj)の様に
rvalueの受け入れ先になる変数が存在していないのが原因でした。

std::move(T&&)は受け入れ先の変数の
ムーブコンストラクタやムーブオペレーターを呼び出すために
特定の変数をrvalueにするための操作でしかないという基本的な理解が抜けていました。

std::move(T&&)の効果についてはcppreference.comにも書かれています。

std::move - cppreference.com:

std::move は、オブジェクト t からムーブしても構わない、つまり、 t から別のオブジェクトへのリソースの効率的な転送を許可する、ということを示すために使用されます。

特に、 std::move はその引数 t を表す xvalue 式を生成します。 これは右辺値参照型への static_cast と正確に同等です。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?