> On Sunday, 6 March 2016 13:25:50 UTC+1, Andrey Semashev wrote:
>
> On 2016-03-06 15:13,
atch...@gmail.com <javascript:> wrote:
> > Is std::unique_ptr supposed to be drop in replacement for raw
> pointers?
> > That is I mean 100% compatibility if I change raw pointers in my
> code to
> > std::unique?
>
> Of course not. Even disregarding the interface differences, unique_ptr
> has different semantics from a raw pointer in general.
On 2016-03-06 15:55,
atch...@gmail.com wrote:
> Andrew, could you explain shortly what are the differences in semantics
> from raw ptr?
The most apparent difference is the ownership model. unique_ptr implies
that the pointer owns the resource and is responsible for freeing it.
Raw pointers do not own the resource.
Next, unique_ptr does not support pointer arithmetics, aside from the
indexing operator for array specializations. Raw pointers do support it
and because of that can be used as iterators.
So, unique_ptr can and should replace raw pointers in one specific use
case - when you use raw pointers to keep reference to an allocated
resource for later deallocation. It won't be a drop-in replacement
because of interface differences. That's not the only application of raw
pointers, and unique_ptr won't be appropriate in the other cases.