C ++ 11에서는 다음 코드를 작성할 수 있습니다.
struct Cat {
Cat(){}
};
const Cat cat;
std::move(cat); //this is valid in C++11
를 호출 std::move하면 개체를 이동하고 싶다는 의미입니다. 즉, 개체를 변경하겠습니다. const개체 를 이동하는 것은 비합리적인데 왜이 std::move동작을 제한 하지 않습니까? 장래에 함정이 되겠죠?
여기에 트랩은 Brandon이 주석에서 언급했듯이 의미합니다.
"내 생각에 그가 몰래 은밀하게"덫을 놓는다 "는 뜻이라고 생각한다. 왜냐하면 그가 깨닫지 못하면 의도 한 것과 다른 사본으로 끝나기 때문이다."
Scott Meyers의 '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이름이 잘못 되었다고 주장 할 수 있습니다.