To demonstrate the semantics of movement, I wrote the following code example with an implicit constructor from int.
struct C { int i_=0; C() {} C(int i) : i_( i ) {} C( const C& other) :i_(other.i_) { std::cout << "A copy construction was made." << i_<<std::endl; } C& operator=( const C& other) { i_= other.i_ ; std::cout << "A copy assign was made."<< i_<<std::endl; return *this; } C( C&& other ) noexcept :i_( std::move(other.i_)) { std::cout << "A move construction was made." << i_ << std::endl; } C& operator=( C&& other ) noexcept { i_ = std::move(other.i_); std::cout << "A move assign was made." << i_ << std::endl; return *this; } };
AND
auto vec2 = std::vector<C>{1,2,3,4,5}; cout << "reversing\n"; std::reverse(vec2.begin(),vec2.end());
With exit
A copy construction was made.1 A copy construction was made.2 A copy construction was made.3 A copy construction was made.4 A copy construction was made.5 reversing A move construction was made.1 A move assign was made.5 A move assign was made.1 A move construction was made.2 A move assign was made.4 A move assign was made.2
Now the opposite shows two two swaps (each using one move destination and two move constructs), but why can't temporary C
objects created from the list of initializers be moved? I thought I had a list of integer initializers, but now I'm wondering if I have an intermediate list of Cs initializers that cannot be ported from (like its const). Is this the correct interpretation? - What's happening?
Live demo
source share