p****n 发帖数: 69 | 1 It is understood that auto_ptr should not be used with STL container.
For example, it is seriously wrong to call sort from header to
sort a vector>, if the implementation uses quick sort and a
pivot, a copy of some element, is used.
Unique_ptr has the same feature of taking ownership when copying as auto_ptr
. I know that unique_ptr's assignment operator uses rvalue reference, but
the same problem in the above example remains: the pivot takes ownership of
some element, which is subsequently destroyed.
Then the question is: is it safe to use unique_ptr with STL container?
I did some search, the answer seems to be yes, but I don't know why. | t****t 发帖数: 6806 | 2 yes, unique_ptr<> is container safe. the point is, if T is not movable, (e.g
. price of moving is too big), then you use a pointer for that.
ptr
of
【在 p****n 的大作中提到】 : It is understood that auto_ptr should not be used with STL container. : For example, it is seriously wrong to call sort from header to : sort a vector>, if the implementation uses quick sort and a : pivot, a copy of some element, is used. : Unique_ptr has the same feature of taking ownership when copying as auto_ptr : . I know that unique_ptr's assignment operator uses rvalue reference, but : the same problem in the above example remains: the pivot takes ownership of : some element, which is subsequently destroyed. : Then the question is: is it safe to use unique_ptr with STL container? : I did some search, the answer seems to be yes, but I don't know why.
|
|