简体   繁体   中英

When is the move constructor called in the `std::move()` function?

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:

  1. When the parameter is passed.
  2. When the cast is performed.
  3. When the result is returned.
  4. 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.

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).

std :: move只是一个类型转换,它告诉编译器该类型是一个右值。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM