Question

Somewhere in my code I have a local std::unique_ptr<T>. I need to do stuff with the object pointed at, and I use a function for that:

std::unique_ptr<T> some_function( std::unique_ptr<T> &t )
{
    // do stuff
    return t;
}

I call the function like this:

std::unique_ptr<T> some_T_ptr( new T(/*args*/) );
vector_of_finalized_Ts.push_back( std::move(some_function(std::move(some_T_ptr))));

Now I wonder, is there a better way to get the necessary functionality? It just seems two moves are pretty superfluous and potentially dangerous. I do have error handling code I'm not showing here, but that's beside the point.

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top