gpt4 book ai didi

c++ - 如何将 slist 'node_ptr' 转换为我自己的节点类型

转载 作者:太空宇宙 更新时间:2023-11-04 11:24:19 25 4
gpt4 key购买 nike

我已经声明了以下 node继承自 boost::intrusive::slist_base_hook<> :

class InputBufferSglNode : public boost::intrusive::slist_base_hook<>

包含这些节点的列表的声明:

class InputBufferSglList : public boost::intrusive::slist<InputBufferSglNode, boost::intrusive::cache_last<true>>

我想从InputBufferSglList得到根节点成员函数,所以我尝试这样做:
InputBufferSglNode* node = this->get_root_node();
但我得到一个错误:

error: cannot initialize a variable of type 'InputBufferSglNode *' with an rvalue of type 'node_ptr' (aka 'boost::intrusive::slist_node<void *> *')

我应该投 node_ptr 吗?至 InputBufferSglNode* ?应该是哪个铸件?

最佳答案

get_root_node 不是记录的 API 的一部分。

你在找什么

InputBufferSglNode& node = *this->begin();

如果你对反向感兴趣:


更新:

我在文档中阅读了更多内容,并且有一种方法可以通过使用列表类型的派生 value_traits 来做你想做的事:

InputBufferSglNode* node = 
InputBufferSglList::value_traits::to_value_ptr(list.get_root_node());

这使用了 ValueTraits interface 是所有侵入式容器的基础。

对于所有常见的节点默认 Hook ,它会导致一些偏移算法(例如,从成员地址到节点地址)。但在实践中,它可能涉及类层次结构的上下转换(尤其是在涉及虚拟基础时)。

对于任何trivial_value_traits to_value_ptrto_node_ptr 都是恒等函数。

关于c++ - 如何将 slist 'node_ptr' 转换为我自己的节点类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27215366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com