C++ 实用程序库 - move 函数



描述

它返回一个右值引用到 arg。

声明

以下是 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;

参数

arg − 它是一个对象。

返回值

它返回一个指向 arg 的右值引用。

异常

基本保证 − 此函数从不抛出异常。

数据竞争

调用此函数不会引入任何数据竞争。

示例

以下示例说明了 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
utility.htm
广告

© . All rights reserved.