If my understanding is correct, the following declarations should both call the copy constructor of T
which takes type of x
as a parameter.
T t = x;
T t(x);
But when I do the same for std::unique_ptr<int>
I get an error with the first declaration, while the second compiles and does what is expected.
std::unique_ptr<int> x = new int();
std::unique_ptr<int> x (new int());
Is there a difference in the two syntax for calling the copy constructor?
Constructor of std::unique_ptr<>
is explicit
, which means, you need to write it in the first case:
std::unique_ptr<int> x = std::unique_ptr<int>(new int());
// or
auto x = std::unique_ptr<int>(new int());
// or make_unique()