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?
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);
// ^^^^^^^^^^^^^^^^^^^^^^^^^^
Adding to the answer from KennyTM
(since C++11)
tr = (decltype(tr)(new int[3]));
Personally I prefer this as it makes updating the type of tr easier. (Only single place to update)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With