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.