Why is the destructor of an object called when it goes out of scope and when it is in a vector ?
For example :
for (i=10; i<10; i++)
object will die out of this scope, this is correct. No,
vector doesn't and couldn't affect its lifetime.
push_back actually copies
object into the
vector. To avoid this, you can use C++11's
emplace_back which allows to construct
This test program confirms no copy, move or extra destruction is performed.
Constructing A End of vec's scope Destructing A