什么时候在`std :: move()`函数中调用move构造函数? [英] When is the move constructor called in the `std::move()` function?

查看:136
本文介绍了什么时候在`std :: move()`函数中调用move构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

函数std::move()定义为

template<typename T>
typename std::remove_reference<T>::type&& move(T && t)
{ 
    return static_cast<typename std::remove_reference<T>::type&&>( t ); 
}

我可以想象在四个地方调用move构造函数:

There are four places where I can imagine the move constructor to be called:

  1. 传递参​​数时.
  2. 执行投射时.
  3. 返回结果时.
  4. 不在std::move()函数本身中,但可能位于返回的引用最终到达的位置.
  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.

我敢打赌第4位,但我不确定100%,因此请解释您的答案.

I would bet for number 4, but I'm not 100% sure, so please explain your answer.

推荐答案

没有进行任何移动构造. std::move()接受引用并返回引用. std::move()基本上只是强制转换.

There is no move construction going on. std::move() accepts a reference and returns a reference. std::move() is basically just a cast.

您的猜测4.是正确的(假设您实际上实际上是在最后调用move构造函数).

Your guess 4. is the right one (assuming that you are actually calling a move constructor in the end).

这篇关于什么时候在`std :: move()`函数中调用move构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆