首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::move

Defined in header <utility>

?

?

template< class T > typename std::remove_reference<T>::type&& move( T&& t );

?

(since C++11) (until C++14)

template< class T > constexpr typename std::remove_reference<T>::type&& move( T&& t );

?

(since C++14)

std::move习以为常指示一个物体t可以“移出”,即允许有效地将资源从t另一个物体。

特别是,std::move产生一个x值表达式标识其参数的t它完全等同于静电[医]转换为rvalue引用类型。

参数

t

-

the object to be moved

返回值

static_cast<typenamestd::remove_reference<T>::type&&>(t)...

例外

noexcept规格:

noexcept

注记

接受rvalue引用参数%28的函数包括移动构造器,,,移动分配运算符,以及常规成员函数,如std::vector::push_back%29被选中,由过载分辨率,当被调用时r值参数%28prvalue例如临时对象或x值例如由std::move29%。如果参数标识了一个拥有资源的对象,这些重载可以选择,但是%27T是必需的,以便移动论点所拥有的任何资源。例如,链接列表的移动构造函数可能会将指针复制到列表的头部并存储。nullptr而不是分配和复制单个节点。

...的名字R值参考变量是lvalue必须转换成x值绑定到接受rvalue引用参数的函数重载,这就是为什么移动构造器和移动分配运算符典型使用std::move*

二次

代码语言:javascript
复制
// Simple move constructor
A(A&& arg) : member(std::move(arg.member)) // the expression "arg.member" is lvalue
{} 
// Simple move assignment operator
A& operator=(A&& other) {
     member = std::move(other.member);
     return *this;
}

二次

一个例外情况是,当函数参数的类型为rvalue引用到类型模板参数%28“转发引用”或“通用引用”%29时,在这种情况下std::forward取而代之的是。

除非另有规定,所有已从其移动的标准库对象都处于有效但未指定的状态。也就是说,只有没有先决条件的函数,例如赋值运算符,才能在对象从以下位置移动后安全地使用:

二次

代码语言:javascript
复制
std::vector<std::string> v;
std::string str = "example";
v.push_back(std::move(str)); // str is now valid but unspecified
str.back(); // undefined behavior if size() == 0: back() has a precondition !empty()
str.clear(); // OK, clear() has no preconditions

二次

此外,使用xvalue参数调用的标准库函数可能假定参数是对对象的唯一引用;如果它是从std::move,不进行混叠检查。特别是,这意味着标准库移动赋值运算符不必执行自分配检查:

二次

代码语言:javascript
复制
std::vector<int> v = {2, 3, 3};
v = std::move(v); // undefined behavior

二次

二次

代码语言:javascript
复制
#include <iostream>
#include <utility>
#include <vector>
#include <string>
 
int main()
{
    std::string str = "Hello";
    std::vector<std::string> v;
 
    // uses the push_back(const T&) overload, which means 
    // we'll incur the cost of copying str
    v.push_back(str);
    std::cout << "After copy, str is \"" << str << "\"\n";
 
    // uses the rvalue reference push_back(T&&) overload, 
    // which means no strings will be copied; instead, the contents
    // of str will be moved into the vector.  This is less
    // expensive, but also means str might now be empty.
    v.push_back(std::move(str));
    std::cout << "After move, str is \"" << str << "\"\n";
 
    std::cout << "The contents of the vector are \"" << v[0]
                                         << "\", \"" << v[1] << "\"\n";
}

二次

可能的产出:

二次

代码语言:javascript
复制
After copy, str is "Hello"
After move, str is ""
The contents of the vector are "Hello", "Hello"

二次

另见

forward (C++11)

forwards a function argument (function template)

move_if_noexcept (C++11)

obtains an rvalue reference if the move constructor does not throw (function template)

move (C++11)

moves a range of elements to a new location (function template)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com