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

ⅰ亾dé卋堺 提交于 2019-12-18 19:12:07

问题


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.


回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!