std::move
来自cppreference.com
|
|
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
| Defined in header <utility>
|
||
| template< class T > typename std::remove_reference<T>::type&& move( T&& t ); |
(C++11 起) | |
std::move获得一个右值引用它的参数。右值引用,否则只能产生临时对象,因此以运行库的代码,通过一个右值引用一个资源拥有的对象有选择权(但不是必需的)“移动”资源的说法更迅速,留下一个空值的参数。库代码需要留下一个有效的参数值,但得到的参数值,除非该类型或函数文件,否则,有没有其他的限制。这意味着,它通常是最明智的,避免使用再次从参数。如果你要使用它,一定要重新初始化它与已知值,在这样做之前.Original:
std::move obtains an rvalue reference to its argument. Rvalue references are otherwise only produced by temporary objects, so library code that's passed an rvalue reference to a resource-owning object has the option (but isn't required) to move the resource out of the argument in order to run more quickly, leaving the argument with an empty value. The library code is required to leave a valid value in the argument, but unless the type or function documents otherwise, there are no other constraints on the resulting argument value. This means that it's generally wisest to avoid using a moved from argument again. If you have to use it again, be sure to re-initialize it with a known value before doing so.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
目录 |
[编辑] 参数
| t | - | 要移动的对象
Original: the object to be moved The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
[编辑] 返回值
static_cast<typename std::remove_reference<T>::type&&>(t)
[编辑] 例外
[编辑] 为例
#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 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"; }
Output:
After copy, str is "Hello" After move, str is "" The contents of the vector are "Hello", "Hello"
[编辑] 复杂性
常数
Original:
Constant
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[编辑] 另请参阅
| (C++11) |
转发函数的参数 Original: forwards a function argument The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (函数模板) |
| (C++11) |
获得一个右值引用的举动构造方法不抛出 Original: obtains an rvalue reference if the move constructor does not throw The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (函数模板) |