Unique Pointer Initialization in C++11

cc++11initializationsmart-pointer

What is the correct initialisation of a smart pointer?

std::unique_ptr<Class> ptr(std::make_unique<Class>());

or

std::unique_ptr<Class> ptr = std::make_unique<Class>();

Is there an implicit copy with the second usage?

Best Answer

The two are equivalent.

The second would (officially) require an implicit copy if (and only if) the type of the initializer differed from the type of the object being initialized. In reality, even in that case most compilers can normally generate code that elides the copy.