What is std::move(), and when should it be used?(什么是 std::move(),什么时候应该使用?)
问题描述
- 这是什么?
- 它有什么作用?
- 应该什么时候使用?
感谢好的链接.
推荐答案
维基百科页面关于 C++11 R 值引用和移动构造函数
- 在 C++11 中,除了复制构造函数之外,对象还可以有移动构造函数.
(除了复制赋值运算符,它们还有移动赋值运算符.) - 如果对象具有右值引用"类型(
Type &&
),则使用移动构造函数而不是复制构造函数. std::move()
是一个强制转换,它产生一个对对象的右值引用,以允许从它移动.
- 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.) - The move constructor is used instead of the copy constructor, if the object has type "rvalue-reference" (
Type &&
). 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(),什么时候应该使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:什么是 std::move(),什么时候应该使用?


基础教程推荐
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09