Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::move

      From cppreference.com
      <cpp‎ |utility
       
       
      Utilities library
       
      Defined in header<utility>
      template<class T>
      typenamestd::remove_reference<T>::type&& move( T&& t)noexcept;
      (since C++11)
      (until C++14)
      template<class T>
      constexprstd::remove_reference_t<T>&& move( T&& t)noexcept;
      (since C++14)

      std::move is used toindicate that an objectt may be "moved from", i.e. allowing the efficient transfer of resources fromt to another object.

      In particular,std::move produces anxvalue expression that identifies its argumentt. It is exactly equivalent to astatic_cast to an rvalue reference type.

      Contents

      [edit]Parameters

      t - the object to be moved

      [edit]Return value

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

      [edit]Notes

      The functions that accept rvalue reference parameters (includingmove constructors,move assignment operators, and regular member functions such asstd::vector::push_back) are selected, byoverload resolution, when called withrvalue arguments (eitherprvalues such as a temporary object orxvalues such as the one produced bystd::move). If the argument identifies a resource-owning object, these overloads have the option, but aren't required, tomove any resources held by the argument. For example, a move constructor of a linked list might copy the pointer to the head of the list and storenullptr in the argument instead of allocating and copying individual nodes.

      Names ofrvalue reference variables arelvalues and have to be converted toxvalues to be bound to the function overloads that accept rvalue reference parameters, which is whymove constructors andmove assignment operators typically usestd::move:

      // Simple move constructorA(A&& arg): member(std::move(arg.member))// the expression "arg.member" is lvalue{} // Simple move assignment operatorA& operator=(A&& other){    member= std::move(other.member);return*this;}

      One exception is when the type of the function parameter is aforwarding reference (which looks like an rvalue reference to type template parameter), in which casestd::forward is used instead.

      Unless otherwise specified, all standard library objects that have been moved from are placed in a "valid but unspecified state", meaning the object's class invariants hold (so functions without preconditions, such as the assignment operator, can be safely used on the object after it was moved from):

      std::vector<std::string> v;std::string str="example";v.push_back(std::move(str));// str is now valid but unspecifiedstr.back();// undefined behavior if size() == 0: back() has a precondition !empty()if(!str.empty())    str.back();// OK, empty() has no precondition and back() precondition is met str.clear();// OK, clear() has no preconditions

      Also, the standard library functions called with xvalue arguments may assume the argument is the only reference to the object; if it was constructed from an lvalue withstd::move, no aliasing checks are made. However, self-move-assignment of standard library types is guaranteed to place the object in a valid (but usually unspecified) state:

      std::vector<int> v={2,3,3};v= std::move(v);// the value of v is unspecified

      [edit]Example

      Run this code
      #include <iomanip>#include <iostream>#include <string>#include <utility>#include <vector> int main(){std::string str="Salut";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 "<<std::quoted(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 "<<std::quoted(str)<<'\n'; std::cout<<"The contents of the vector are {"<<std::quoted(v[0])<<", "<<std::quoted(v[1])<<"}\n";}

      Possible output:

      After copy, str is "Salut"After move, str is ""The contents of the vector are {"Salut", "Salut"}

      [edit]See also

      (C++11)
      forwards a function argument and use the type template argument to preserve its value category
      (function template)[edit]
      converts the argument to an xvalue if the move constructor does not throw
      (function template)[edit]
      (C++11)
      moves a range of elements to a new location
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/move&oldid=170528"

      [8]ページ先頭

      ©2009-2025 Movatter.jp