问题
The function std::move()
is defined as
template<typename T>
typename std::remove_reference<T>::type&& move(T && t)
{
return static_cast<typename std::remove_reference<T>::type&&>( t );
}
There are four places where I can imagine the move constructor to be called:
- When the parameter is passed.
- When the cast is performed.
- When the result is returned.
- Not in the
std::move()
function itself but possibly at the place where the returned reference ultimately arrives.
I would bet for number 4, but I'm not 100% sure, so please explain your answer.
回答1:
There is no move construction going on. std::move()
accepts a reference and returns a reference. std::move()
is basically just a cast.
Your guess 4. is the right one (assuming that you are actually calling a move constructor in the end).
回答2:
std::move is just a type cast, it tells the compiler that the type is an rvalue.
来源:https://stackoverflow.com/questions/14749587/when-is-the-move-constructor-called-in-the-stdmove-function