To add the element a copy constructor is invoked on a temporary object. After the push_back()
the temporary object is destroyed - that't the first destructor call. Then vector
instance goes out of scope and destroys all the elements stored - that's the second destructor call.
This will show you what's happening:
struct A {
A() { cout << "contruction\n"; }
A(A const& other) { cout << "copy construction\n"; }
~A() { cout << "destruction\n"; }
};
int main() {
vector<A> t;
t.push_back(A());
}
The destructor is called once when the temporary sent to push_back
is destroyed and once when the element in t
is destroyed.
There are two destructor calls because there are two objects: the argument to push_back
, and the newly added element within vector
t
.
STL containers store copies. In your example the element added to the vector
by push_back
is copy constructed from the argument passed to push_back
. The argument is A()
, which is a temporary object, see here (variant 4).
Expanding the answer a bit, altough you havent explicitely asked for it: It might be useful to know when the temporary is destroyed. The standard (N4140) sais it pretty clearly in 12.2 p3:
... Temporary objects are destroyed as the last step in evaluating the full-expression (1.9) that (lexically) contains the point where they were created...
Side note: If you use emplace_back
there is only one object. The new element in the container is directly constructed from the arguments to emplace_back. Many STL container learned an emplace variant in C++11.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With