|
|
|
Specifies that an instance of the type can be constructed from anrvalue argument.
Contents |
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.
Expression | Post-conditions |
---|---|
T u= rv; | The value ofu is equivalent to the value ofrv before the initialization.The new value of |
T(rv) | The value ofT(rv) is equivalent to the value ofrv before the initialization.The new value of |
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 |
Extended content |
---|
|
(C++11)(C++11)(C++11) | checks if a type can be constructed from an rvalue reference (class template)[edit] |
(C++20) | specifies that an object of a type can be move constructed (concept)[edit] |