Reputation: 43331
std::unique_ptr<int> ptr;
ptr = new int[3]; // error
error C2679: binary '=' : no operator found which takes a right-hand operand of type 'int *' (or there is no acceptable conversion)
Why this is not compiled? How can I attach native pointer to existing unique_ptr instance?
Upvotes: 10
Views: 11412
Reputation: 523784
Firstly, if you need an unique array, make it
std::unique_ptr<int[]> ptr;
// ^^^^^
This allows the smart pointer to correctly use delete[]
to deallocate the pointer, and defines the operator[]
to mimic a normal array.
Then, the operator=
is only defined for rvalue references of unique pointers and not raw pointers, and a raw pointer cannot be implicitly converted to a smart pointer, to avoid accidental assignment that breaks uniqueness. Therefore a raw pointer cannot be directly assigned to it. The correct approach is put it to the constructor:
std::unique_ptr<int[]> ptr (new int[3]);
// ^^^^^^^^^^^^
or use the .reset
function:
ptr.reset(new int[3]);
// ^^^^^^^ ^
or explicitly convert the raw pointer to a unique pointer:
ptr = std::unique_ptr<int[]>(new int[3]);
// ^^^^^^^^^^^^^^^^^^^^^^^ ^
If you can use C++14, prefer the make_unique
function over using new
at all:
ptr = std::make_unique<int[]>(3);
// ^^^^^^^^^^^^^^^^^^^^^^^^^^
Upvotes: 29