is a smart pointer that owns and manages another object through a pointer and disposes of that object when the goes out of scope.
The object is disposed of using the associated deleter when either of the following happens:
- the managing object is destroyed
- the managing object is assigned another pointer via operator= or reset().
The object is disposed of using a potentially user-supplied deleter by calling get_deleter()(ptr). The default deleter uses the delete operator, which destroys the object and deallocates the memory.
A may alternatively own no object, in which case it is called empty.
There are two versions of :
1) Manages a single object (e.g. allocated with new)
2) Manages a dynamically-allocated array of objects (e.g. allocated with new)
The class satisfies the requirements of and , but not the requirements of either or .
|- must be or lvalue reference to a or lvalue reference to function, callable with an argument of type|
Only non-const can transfer the ownership of the managed object to another . If an object's lifetime is managed by a const std::unique_ptr, it is limited to the scope in which the pointer was created.
is commonly used to manage the lifetime of objects, including:
- providing exception safety to classes and functions that handle objects with dynamic lifetime, by guaranteeing deletion on both normal exit and exit through exception
- passing ownership of uniquely-owned objects with dynamic lifetime into functions
- acquiring ownership of uniquely-owned objects with dynamic lifetime from functions
- as the element type in move-aware containers, such as std::vector, which hold pointers to dynamically-allocated objects (e.g. if polymorphic behavior is desired)
may be constructed for an incomplete type, such as to facilitate the use as a handle in the pImpl idiom. If the default deleter is used, must be complete at the point in code where the deleter is invoked, which happens in the destructor, move assignment operator, and member function of . (Conversely, std::shared_ptr can't be constructed from a raw pointer to incomplete type, but can be destroyed where is incomplete). Note that if is a class template specialization, use of as an operand, e.g. !p requires 's parameters to be complete due to ADL.
If is a derived class of some base , then std::unique_ptr<T> is implicitly convertible to std::unique_ptr<B>. The default deleter of the resulting std::unique_ptr<B> will use operator delete for , leading to undefined behavior unless the destructor of is virtual. Note that std::shared_ptr behaves differently: std::shared_ptr<B> will use the operator delete for the type and the owned object will be deleted correctly even if the destructor of is not virtual.
Unlike std::shared_ptr, may manage an object through any custom handle type that satisfies . This allows, for example, managing objects located in shared memory, by supplying a that defines or another fancy pointer.
|pointer||std::remove_reference<Deleter>::type::pointer if that type exists, otherwise . Must satisfy|
|element_type||, the type of the object managed by this|
|deleter_type||, the function object or lvalue reference to function or to function object, to be called from the destructor|
Run this code
需要注意的是，在使用前最好仔细的阅读 man fmemopen的内容，尤其是在wa两种模式下，函数的行为。
在glibc 2.9之前的版本暂不支持模式，这个问题带来的影响是，若以binary模式打开，进行fseek(stream, 0L, SEEK_END)时，pos指针很可能不会到真正的尾，而是buffer中第一个 处。对比glibc 2.5 和glibc 2.21的代码可以知道：
//glibc 2.21 case SEEK_END: np = (c->binmode ? c->size : c->maxpos) - *p; break;
//glibc 2.5 case SEEK_END: np = c->maxpos - *p; break;
warning: assignment makes pointer from integer without a cast
这个警告的解决方式是在include stdio.h头文件之前添加一行，正如man page上所说。