问题描述
Howard Hinnant unique_ptr
也可以使用自定义存储类型。他提到一个例子共享内存。
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)?
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
}
我怀疑 // ...
必须扩展, test()
可能会在真正的自定义存储类型
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
- 实际上已经使用此自定义,
- 和上下文,
- 代码(
// ...
可能) - 使用它? (
test()
可能)
- 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)
推荐答案
您可能对,用作 unique_ptr
中此自定义点的激励用例。 offset_ptr
是一个完全开发的指针类型,可以使用简单的typedef安装到自定义的删除程序中。它的用例是将 unique_ptr
放入共享内存。
You might be interested in boost::offset_ptr which served as the motivating use case for this customization point in unique_ptr
. offset_ptr
is a fully developed pointer type which could be installed into a custom deleter using a simple typedef. Its use case is to put unique_ptr
into shared memory.
这篇关于unique_ptr自定义存储类型示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!