Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      C++ named requirements:MoveConstructible(since C++11)

      From cppreference.com
      <cpp‎ |named req
       
       
      C++ named requirements
       

      Specifies that an instance of the type can be constructed from anrvalue argument.

      Contents

      [edit]Requirements

      The typeT satisfiesMoveConstructible if

      Given

      • rv, anrvalue expression of typeT,
      • u, an arbitrary identifier.

      The following expressions must be valid and have their specified effects.

      ExpressionPost-conditions
      T u= rv;The value ofu is equivalent to the value ofrv before the initialization.

      The new value ofrv is unspecified.

      T(rv)The value ofT(rv) is equivalent to the value ofrv before the initialization.

      The new value ofrv is unspecified.

      [edit]Notes

      A class does not have to implement amove constructor to satisfy this type requirement: acopy constructor that takes aconst T& argument can bind rvalue expressions.

      If aMoveConstructible class implements a move constructor, it may also implementmove semantics to take advantage of the fact that the value ofrv after construction is unspecified.

      Extended content

      Being aMoveConstructible class impliesstd::is_move_constructible but not vice versa sincestd::is_move_constructible will only check for the ability to call the constructor with the correct arguments, not a post-condition value.

      Run this code
      #include <iostream> struct S{int n;    S(int in): n{in}{}    S(S&& other){ n= other.n+1;}};static_assert(std::is_move_constructible_v<S>); int main(){    S v{1};std::cout<<"v.n = "<< v.n<<'\n';    S u= std::move(v); // Class `S` doesn't satisfy a MoveConstructible requirement// The value of `u` is NOT equivalent to the value of `v` before the `u` initializationstd::cout<<"u.n = "<< u.n<<'\n';}

      Output:

      v.n = 1u.n = 2

      [edit]References

      Extended content
      • C++23 standard (ISO/IEC 14882:2024):
      • 16.4.4.2 Template argument requirements [utility.arg.requirements]

      [edit]See also

      checks if a type can be constructed from an rvalue reference
      (class template)[edit]
      specifies that an object of a type can be move constructed
      (concept)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/named_req/MoveConstructible&oldid=170057"

      [8]ページ先頭

      ©2009-2025 Movatter.jp