While Smart pointers manage the lifetime of an object being pointed to, it is often still useful to have access to the underlying raw pointer.
In fact if we read Herb Sutter's GotW #91 Solution: Smart Pointer Parameters he recommends passing parameters by pointer or reference when the function is agnostic to the lifetime of the parameter, he says:
Pass by * or & to accept a widget independently of how the caller is
managing its lifetime. Most of the time, we don’t want to commit to a
lifetime policy in the parameter type, such as requiring the object be
held by a specific smart pointer, because this is usually needlessly
restrictive.
and we should pass by unique_ptr when the function is a sink:
Passing a unique_ptr by value is only possible by moving the object
and its unique ownership from the caller to the callee. Any function
like (c) takes ownership of the object away from the caller, and
either destroys it or moves it onward to somewhere else.
and finally pass a unique_ptr
by reference when we can potentially modify it to refer to a different object:
This should only be used to accept an in/out unique_ptr, when the
function is supposed to actually accept an existing unique_ptr and
potentially modify it to refer to a different object. It is a bad way
to just accept a widget, because it is restricted to a particular
lifetime strategy in the caller.
Of course, we are required to get the underlying pointer if we have to interface with C libraries that take pointers.
In your specific example:
int* myPtr = intPtr.get();
There is no transfer of ownership to another smart pointer so there are no problems as long as you don't attempt to delete
the pointer via myPtr
. You can transfer ownership to another unique_ptr
by moving it:
std::unique_ptr<int> intPtr2( std::move( intPtr ) ) ;
int* myPtr = intPtr.operator->();
would be harder to read?unique_ptr
has no choice but to expose the raw pointer in some way - otherwise, how would you access anything pointed to? – Igor Tandetnik Aug 18 '14 at 19:28unique_ptr
doesn't exist to prevent aliasing. It exists to allow one (and only one) "owner" of object lifetime.myPtr
doesn't "own" the lifetime of theint
. – Cornstalks Aug 18 '14 at 19:31