Is this safe or should I pass std::unique_ptr instead of using new?
void Function( Object* object )
{
myObjects.push_back( std::unique_ptr< Object >( object ); //myObjects is a vector of std::unique_ptr< Object >
}
Function( new object );
Or would this be better?
void Function( std::unique_ptr< Object > object )
{
myObjects.push_back( std::move( object ) ); //myObjects is a vector of std::unique_ptr< Object >
}
Function( std::unique_ptr< Object >( new object ) );
I ask because I just started making use of smart pointers and want to make sure I'm doing it right.
You're doing it right in the second code. There is however a potential probem with temporary smart pointers if multiple ones are used:
Function2(std::unique_ptr<A>(new A), std::unique_ptr<B>(new B));
This code is not exception-safe, because order of execution of the expression parts is not determined by the standard. A compiler is allowed to execute first new A and then new B, and only then call the unique_ptr constructors. If the B constructor throws an exception, then you have a memory leak, because the A object is never deleted. One way to fix the problem is to use local variables instead of temporary expressions.
If your compiler already supports that, you can also use std::make_unique():
Function( std::make_unique<Object>() );
Since it encapsulates the call to the new operator, it nicely solves the exception issue.