Reputation: 171
I read the book [The C++ Standard Library Second Edition] and found the section below:
namespace std {
template <typename T, typename D>
class unique_ptr<T[], D>
{
public:
typedef ... pointer; // may be D::pointer
typedef T element_type;
typedef D deleter_type;
...
};
}
The element type T might be void so that the unique pointer owns an object with an unspecified type, like void* does. Note also that a type pointer is defined, which is not necessarily defined as T*. If the deleter D has a pointer typedef, this type will be used instead. In such a case, the template parameter T has only the effect of a type tag, because there is no member as part of class unique_ptr<> that depends on T; everything depends on pointer. The advantage is that a unique_ptr can thus hold other smart pointers.
I still cant not understand the purpose of "everything depends on pointer" after i reading this section.Is there anyone could provide some samples kindly?Thanks.
Upvotes: 4
Views: 1143
Reputation: 218700
LWG issue 673 added pointer
to the unique_ptr
specification. It contains this bullet point for motivation:
- Efforts have been made to better support containers and smart pointers in shared memory contexts. One of the key hurdles in such support is not assuming that a pointer type is actually a
T*
. This can easily be accomplished forunique_ptr
by having the deleter define the pointer type:D::pointer
. Furthermore this type can easily be defaulted toT*
should the deleterD
choose not to define a pointer type (example implementation here[broken link]). This change has no run time overhead. It has no interface overhead on authors of custom delter types. It simply allows (but not requires) authors of custom deleter types to define a smart pointer for the storage type ofunique_ptr
if they find such functionality useful.std::default_delete
is an example of a deleter which defaults pointer toT*
by simply ignoring this issue and not including apointer typedef
.
See boost::offset_ptr
for an example smart pointer that can reference shared memory.
Upvotes: 6
Reputation: 1686
unique_ptr
is designed in such a way that, if you don't provide a deleter in the declaration, thanks to default template parameters it uses a default_deleter(std::default_delete<T>
) that just "delete
" your pointer. It uses SFINAE to look for the definition of "pointer" in your Deleter Type. If your unique_ptr
holds another smart_ptr(which have the "pointer" defined), then in the destructor, it calls the deleter of the unique_ptr
that you're holding.
Example:
class BufferClass
{
public:
BufferClass()
{
// init
}
~BufferClass()
{
delete [] buffer;
}
int *buffer;
};
// a short declaration for unique_ptr<BufferClass>
typedef unique_ptr<BufferClass> BufferPointer;
BufferPointer myBuffer(new BufferClass(10)); // myBuffer holds BufferClass.
// when myBuffer goes out of scope, u'll have BufferClass::~BufferClass called.
//let's say you want to store a unique_ptr in another unique_ptr.
unique_ptr<BufferPointer> myBufferPointerPointer(&myBuffer);
// so when myBufferPointerPointer goes out of scope, BufferClass::~BufferClass will be called eventually
Upvotes: -1