具有通用指针类型的自链接类的类设计
我目前正在修改一个复杂的类,该类的节点指向自身,就像链接列表或图形一样。我希望它通过 boost::interprocess 函数在共享内存中使用。现在我正在寻找重新设计它的方法,使其保持通用并且涉及最少的更改。
template< class T >
class node {
public:
T data;
node* prev;
node* next;
};
重新设计应使用 boost::interprocess::allocator 分配器,以便隐式使用 boost::interprocess::offset_ptr 类型的相对智能指针。我认为它应该涉及第二个模板参数,因为
template< class T, class alloc_type = std::allocator< node< T > > >
class node {
public:
T data;
typename alloc_type::pointer prev;
typename alloc_type::pointer next;
};
就像引用一样,由于循环依赖关系,该参数当然不起作用。
我希望能够从 C++ 类模板专家那里获得一些关于实现它的最佳方法的帮助。我查看了启用共享内存的增强容器,但它们正在以相当复杂的方式解决它,涉及多个外部类。
约翰
I am currently modifying a complex class that has nodes pointing to themselves just like linked lists or graphs. I want it to be used in shared memory using boost::interprocess functions. Now I am looking for the way to redesign it such that it stays generic and involves least changes.
template< class T >
class node {
public:
T data;
node* prev;
node* next;
};
The redesign should make use of the boost::interprocess::allocator allocator in order to implicitly use the relative smart pointers of type boost::interprocess::offset_ptr. I thought it should involve a second template parameter like
template< class T, class alloc_type = std::allocator< node< T > > >
class node {
public:
T data;
typename alloc_type::pointer prev;
typename alloc_type::pointer next;
};
which of course doesn't work due to cyclic dependencies just as with references.
I hope a can get some help from a C++ class template pro on the best way to implement it. I had a look at the boost shared memory enabled containers but they are solving it in a rather complicated manner that involves several external classes.
Joh
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,这行不通,因为类型
node
正在定义,但尚未完成,但您将其作为类型参数传递给allocator
。因此问题来了!另外,
node
是一个类模板,但是当您将其传递给allocator
时,您没有为node
提供类型参数。但不用担心,即使你通过了,它也不会起作用,因为类型node
尚未完成(正如我之前所说)。除此之外,您还有另一个问题,
这里您需要
typename
作为Yes, that will not work, because the type
node
is being defined and it's not yet complete, but you're passing it toallocator
as type parameter. Hence the problem!Also,
node
is a class template, but when you're passing it toallocator
you're not providing type argument fornode
. But dont worry, even if you pass, it will not work since typenode
is not yet complete (as I said before).Besides that you've another problem,
Here you need
typename
as在我看来,分配器类属于容器,而不是元素。我意识到,对于像您这样的侵入式容器,它们可以是相同的。但是,我们可以把它排除掉吗?
怎么样:
当然,我不知道如何将
std::allocator<>
传递给node<>
,但也许你不需要它。您介意在类节点
中间指定分配器的名称吗?或者,我们可以让您的分配器成为
nodePtr
的默认分配器?It seems to me that the allocator class belongs to the container, not the element. I realize that, for intrusive containers like yours, they can be the same. But, can we factor it back out?
How about:
Of course, I can't figure out how to pass
std::allocator<>
tonode<>
, but maybe you don't need that. Do you mind specifying your allocator's name in the middle ofclass node
?Or maybe, we could make your allocator the default allocator for
nodePtr
?