LoginSignup
8
8

More than 5 years have passed since last update.

std::moveはムーブとは限らない

Last updated at Posted at 2014-02-23

ブログにまとめるのが面倒くさいのでメモ。std::moveは必ずしも「ムーブ」を強制せず、無条件(unconditionally) rvalueキャストを行う。

http://scottmeyers.blogspot.jp/2012/11/on-superfluousness-of-stdmove.html
http://channel9.msdn.com/Events/GoingNative/2013/An-Effective-Cpp11-14-Sampler

例外ケース1

ムーブコンストラクタ/代入演算子を提供しないクラス。コピー動作にフォールバックする。

struct X {
  X(const X&) = default;
  // ムーブコンストラクタなし
};

X v0;
X v1(std::move(v0));  // コピー

例外ケース2

constオブジェクトからのムーブは行われず、コピー動作にフォールバックする。(一般に、ムーブコンストラクタ/代入演算子は非const rvalue参照型X&&を引数にとり、コピーコンストラクタ/代入演算子はconst lvalue参照型const X&を引数にとるため。)

struct X {
  X(const X&) = default;
  X(X&&) = default;
};

void fuunc(const X arg) {
  X v(std::move(arg));  // コピー
}
8
8
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
8
8