Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't move vector trigger moving internal elements?

Tags:

c++

For the following code, I am confused about p3 vector, why move operators are not triggered for Test? For p1 and p2, I can understand that all the elements need to copied be over.

Does that mean when we move a vector, it modifies the vector object so the elements of the vector will leave untouched. Whereas when we copy a vector, we have to copy every single element. Is there a vector operation where the move operator of an element will be trigger?

#include <iostream>
#include <vector> 

using namespace std;

class Test {
  public: 
    Test(int a) {
        std::cout << " default " << std::endl;
    }
    
    Test(const Test& o) {
        std::cout << " copy ctor " << std::endl;
    }
    
    Test& operator=(const Test& o) {
        std::cout << " copy assign " << std::endl;
        return *this;
    }
    
    Test(Test&& o) {
        std::cout << " move ctor" << std::endl;
    }
    
    Test& operator=(Test&& o) {
        std::cout << " move assign " << std::endl;
        return *this;
    }
};

int main()
{
  std::cout << " p: " << std::endl;
  std::vector<Test> p = {Test(0), Test(0)}; 
  std::cout << std::endl;
   
  std::cout << " vec p1 " << std::endl;
  std::vector<Test> p1 = p;
  std::cout << std::endl;
    
  std::cout << " vec p2 " << std::endl;
  std::vector<Test> p2;
  p2.reserve(2);
  p2.emplace_back(0);
  p2.emplace_back(0);
  std::cout << std::endl;
   
  std::cout << " vec p3 " << std::endl;
  std::vector<Test> p3 = std::move(p);
}

Output:

p: 
default 
default 
copy ctor 
copy ctor 

vec p1 
copy ctor 
copy ctor 

vec p2 
default 
default 

vec p3 
like image 233
Zack Avatar asked Apr 19 '26 12:04

Zack


1 Answers

A vector is essentially a pointer to an array. Moving the vector simply swaps the array pointers between the two vectors, the arrays themselves are unchanged.

like image 193
Alan Birtles Avatar answered Apr 22 '26 02:04

Alan Birtles



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!