Please consider this code:
class A
{
};
int main()
{
std::vector test;
test.push_back(A());
}
The constructor and destructor
Use emplace_back.
std::vector<A> test;
test.emplace_back();
//test.emplace_back(constructor, parameters);
This way, A
will be constructed in-place, so no copy or move will occur.
Edit: To clarify on the comments on the question - No, this will not change from push_back
if you pass it a temporary. For instance,
test.emplace_back(A{});
Will, in C++11, cause a temporary A to be constructed, moved and destroyed, as if you used push_back
.