C++ how to avoid calling destructor

Started by
31 comments, last by iMalc 11 years, 2 months ago

Huge discussion for when he probably just wants something like this:


std::vector<some_class> array;

void pushback_function()
{
    some_class temp;
    array.push_back(std::move(temp));
}

... to avoid an extraneous copy constructor and destructor call, but not eliminate destructors everywhere.

Advertisement
That still has an extraneous construction and destruction, nor does it necessarily avoid an expensive copy operation. std::move() isn't magic. It can only move objects that have a properly defined move constructor. If you have std::move() available then you should also have emplace_back() which will avoid the entire temporary object in the first place.
Any chance that we can see the real code yet?

I think there's a high probability that the entire problem here is a lack of following the Rule of three.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement