Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unique_ptr operator=

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?

like image 456
Alex F Avatar asked Feb 22 '12 11:02

Alex F


2 Answers

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);
//    ^^^^^^^^^^^^^^^^^^^^^^^^^^
like image 126
kennytm Avatar answered Oct 03 '22 13:10

kennytm


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)

like image 31
bashrc Avatar answered Oct 03 '22 13:10

bashrc