3

I have read a few (pesudo) implementions of std::move(). And all are just casting away the reference of the parameter and then returning it as a rvalue reference.

It doesn't do anything more than that.

However, I am curious: 1. whether it does more than that. 2. whether standard explicitly states that so that the caller should be aware the side effect.

In the book The C++ Programming Language 4th edition, it states "move(x) marks x for destruction so that move(x) should be used with care".

Does it mean that there is a side effect imposed by the standard and so compiler can do certain optimization?

Thanks in advance.

Hei
  • 1,844
  • 3
  • 21
  • 35

2 Answers2

5

Yes, that's exactly as it is described in the standard. In N4659 (which is last draft I found)

it says in §23.2.5

template <class T> constexpr remove_reference_t<T>&& move(T&& t) noexcept;

Returns: static_cast<remove_reference_t<T>&&>(t)

It doesn't mark anything for destruction, and it doesn't change the object but object may be changed in function that accepts rvalue (such as move constructor, move assignment operator)

RiaD
  • 46,822
  • 11
  • 79
  • 123
  • So if I have void foo(C&& c); and foo(std::move(someC)); someC will be marked for destruction after foo() returns? Thanks in advance! – Hei Nov 19 '17 at 08:03
  • there's no such thing "marking for destruction". this foo may have change the object someC (leaving it in destructable state, e.g making it empty like in case of vector). It will be destructed the same way it would otherwise (e.g when leaving the scope). – RiaD Nov 19 '17 at 20:57
1

Yes, std::move is a bit of a misnomer as it doesn't actually move anything. It is used to indicate that an object may be "moved from".

It does this by casting the object to a T&&. cppreference states the return value is static_cast<typename std::remove_reference<T>::type&&>(t). (btw, that is exactly what VS2017 does)

I don't know precisely what the standard says on the matter.

kmdreko
  • 42,554
  • 6
  • 57
  • 106