0

When i put the class in the vector and define the move operator, i can see that the values ​​are moved when the vector is reallocated.

if So, does it move if you add an integer instead of a class and reallocate it?

enter image description here

Hwan E
  • 566
  • 2
  • 13
  • For simple types as `int` or `double`, does it really matter? Why are you wondering? Is there some other problem underpinning this question? If there's another underlying problem, please ask about it directly instead. – Some programmer dude Nov 25 '19 at 12:46
  • You can't move an integer. There is nothing to move. – Evg Nov 25 '19 at 12:47
  • And besides, how do you "move" an integer value? It's not really possible to move the bits in memory representing the value. – Some programmer dude Nov 25 '19 at 12:47

1 Answers1

4

Fundamental types such as integers don't have any constructors. As such, they don't have move constructors. As such, they are not "moved". The algorithm may still use std::move to convert the operand of the assignment into an xvalue, but that is still a copy assignment.

Conceptually, a move is a shallow copy, with added enforcement of class invariants. Integer is not a referential type, so there is no distinction between shallow and deep copy. It is also not a class and has no invariants.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • And it's worth noting that if you try to move a class that doesn't provide move operations, copy operations are used instead :) – Bktero Nov 25 '19 at 13:07
  • Thanks, the memory overhead by the copy constructor is gone. – Hwan E Nov 25 '19 at 13:08