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?
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.
T t = T();
is required to have a valid and accessible copy constructor, but the standard requires that no copy constructor is called.
Commented
Mar 22, 2016 at 3:11
T t = T();
: "If the initialization is direct-initialization, or if it is copy-initialization where the cv-unqualified version of the source type is the same class as, or a derived class of, the class of the destination, constructors are considered. [...overloads are resolved, then] The constructor so selected is called to initialize the object, with the initializer expression or expression-list as its argument(s)." This doesn't require an available copy ctor.
Commented
Mar 22, 2016 at 4:20
auto ptr = std::make_unique<Class>();
? DRY.