move
描述 (Description)
它返回arg的右值引用。
声明 (Declaration)
以下是std :: move函数的声明。
template <class T>
typename remove_reference<T>::type&& move (T&& arg) noexcept;
C++11
template <class T>
typename remove_reference<T>::type&& move (T&& arg) noexcept;
参数 (Parameters)
arg - 这是一个对象。
返回值 (Return Value)
它返回一个引用arg的右值引用。
异常 (Exceptions)
Basic guarantee - 此函数永远不会抛出异常。
数据竞争 (Data races)
调用此函数不会引入任何数据争用。
例子 (Example)
在下面的例子中解释了std :: move函数。
#include <utility>
#include <iostream>
#include <vector>
#include <string>
int main () {
std::string foo = "It is a foo string";
std::string bar = "It is a bar string";
std::vector<std::string> myvector;
myvector.push_back (foo);
myvector.push_back (std::move(bar));
std::cout << "myvector contains:";
for (std::string& x:myvector) std::cout << ' ' << x;
std::cout << '\n';
return 0;
}
让我们编译并运行上面的程序,这将产生以下结果 -
myvector contains: It is a foo string It is a bar string