Should I assign or reset a unique_ptr?
Solution 1:
From the docs of unique_ptr
's operator=
:
Transfers ownership of the object pointed to by r to *this as if by calling
reset(r.release())
followed by an assignment fromstd::forward<E>(r.get_deleter())
.
And all you need of that is the reset
call, so it's simpler to just call it directly
Solution 2:
The proper way to do this (that you didn't list) is to use the constructor of owned
:
owner() : owned(new someObject())
{}
Apart from that I'd prefer reset
as you don't create a useless intermediate instance in that case (even though there might be no difference on the machine level as the optimizer can do a lot there).