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(),什么时候应该使用它?
基础教程推荐
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- c++ STL设置差异 2022-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- 提升 ASIO 流缓冲 2021-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
