The two statements aren't equivalent. The difference is:
Your second version is calling the
unique_ptr
constructor by providing an existing raw pointer to the object. It only encapsulates that object pointer in aunique_ptr
.The first version is supposed to be provided with the constructor arguments for your object, and it will create the object for you in a safe way. It both constructs and encapsulates an object in a
unique_ptr
.
The problem is that your factory already allocates the object and returns a raw pointer to it. At that point it doesn't make sense anymore to use make_unique
since the purpose of that function is to avoid dealing with raw pointers completely, i.e. construct-and-wrap in one go.
You have two options here:
Stick to your second version.
Change the factory such that it returns a
unique_ptr
by usingmake_unique
in its implementation. But it could make sense to also provide ashared_ptr
-version of the same factory. Something likecreateUnique
+createShared
, and maybe keep the existingcreate
which returns a raw pointer. That's a question of code style however.