I\'m reading C++ Primer, 3rd Ed (Lippman and Lajoie) and it\'s saying that when a vector needs to be reallocated in order to make space for more elements added with push_b
Here's probably the simplest (but rather contrived) example:
class foo
{
int i;
int* pi; // always points to i
};
Here, the copy constructor would maintain the invariant that pi
points to i
. The compiler itself wouldn't be able to figure out this relationship on its own, hence the need to call the copy constructor.
Can someone give me a simple example of a class which shouldn't be moved bit-by-bit?
By the standard, doing a memcpy
on any class that isn't a POD in C++03 (or trivially copyable in C++11) would qualify. memcpy
ing non-PODs (or non-trivially copyable) invokes undefined behavior; therefore an actual copy (or in C++11, move) constructor must be used.
So std::vector
itself applies, since it is not a POD type (and in C++11, it's not trivially copyable).