Howard Hinnant explained that unique_ptr can also use a custom storage type. He mentions as an example “shared memory“.
He only gives the rough idea (which is fine for a quick intro). But can anyone complete his example for a “custom storage type” (be it shared memory or not)?
To support placing
unique_ptrinto shared memory, the custom deleter can contain a custom pointer type (typically not a real pointer in shared memory applications). One simply places a nested type called pointer which emulates pointer behavior within your deleter, publicly accessible:
template <class T>
class MyDeleter
{
public:
class pointer
{
public:
friend bool operator==(pointer x, pointer y);
friend bool operator!=(pointer x, pointer y);
// ...
};
void operator()(pointer p);
};
void test()
{
unique_ptr<int, MyDeleter<int> > p;
MyDeleter<int>::pointer p2 = p.get(); // A custom pointer type used for storage
}
I suspect that // ... must be extended, and test() will probably do some additional things in a real “custom storage type” example.
Can someone tell me where he/she
- has actually already used this customization,
- and in that context,
- which what customization code, (at
//...probably) - and how client code uses it? (at
test()probably)
You might be interested in boost::offset_ptr which served as the motivating use case for this customization point in
unique_ptr.offset_ptris a fully developed pointer type which could be installed into a custom deleter using a simple typedef. Its use case is to putunique_ptrinto shared memory.