什么是 std::move(),什么时候应该使用? [英] What is std::move(), and when should it be used?

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

问题描述

  1. 这是什么?
  2. 它有什么作用?
  3. 应该什么时候使用?

感谢好的链接.

推荐答案

维基百科页面关于 C++11 R 值引用和移动构造函数

  1. 在 C++11 中,除了复制构造函数之外,对象还可以有移动构造函数.
    (除了复制赋值运算符,它们还有移动赋值运算符.)
  2. 如果对象具有右值引用"类型(Type &&),则使用移动构造函数而不是复制构造函数.
  3. std::move() 是一个强制转换,它产生一个对对象的右值引用,以允许从它移动.
  1. In C++11, in addition to copy constructors, objects can have move constructors.
    (And in addition to copy assignment operators, they have move assignment operators.)
  2. The move constructor is used instead of the copy constructor, if the object has type "rvalue-reference" (Type &&).
  3. std::move() is a cast that produces an rvalue-reference to an object, to enable moving from it.

这是一种避免复制的新 C++ 方法.例如,使用移动构造函数,std::vector 可以将其指向数据的内部指针复制到新对象,使移动的对象处于移动状态,因此不会复制所有数据.这将是 C++ 有效的.

It's a new C++ way to avoid copies. For example, using a move constructor, a std::vector could just copy its internal pointer to data to the new object, leaving the moved object in an moved from state, therefore not copying all the data. This would be C++-valid.

尝试使用谷歌搜索移动语义、右值、完美转发.

Try googling for move semantics, rvalue, perfect forwarding.

这篇关于什么是 std::move(),什么时候应该使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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