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
廣告