C ++ 11では、次のコードを記述できます。
struct Cat {
Cat(){}
};
const Cat cat;
std::move(cat); //this is valid in C++11
を呼び出すとstd::move
、オブジェクトを移動することを意味します。つまり、オブジェクトを変更します。移動するにはconst
オブジェクトは不合理ですが、なぜstd::move
この動作を制限しないのですか?将来的には罠になりますよね?
ここでトラップとは、ブランドンがコメントで言及したとおりです。
「彼はそれが彼をこっそりとこっそりと「罠にかける」ことを意味していると思う。もし彼が気付かないと、彼は結局彼が意図したものではないコピーで終わるからだ。」
スコットマイヤーズの著書「Effective Modern C ++」で、彼は例を挙げています。
class Annotation {
public:
explicit Annotation(const std::string text)
: value(std::move(text)) //here we want to call string(string&&),
//but because text is const,
//the return type of std::move(text) is const std::string&&
//so we actually called string(const string&)
//it is a bug which is very hard to find out
private:
std::string value;
};
オブジェクトのstd::move
操作が禁止されていればconst
、バグを簡単に見つけることができますよね?
CAT cat2 = std::move(cat);
をCAT
サポートしていると仮定して、を試してください。
std::move
単なるキャストであり、実際には何も動かしません
std::move
それ自体はオブジェクトに対して何もしません。std::move
名前の付け方が不十分だと主張する人もいます。