使用boost::intrusive_ptr与嵌套类
Using boost::intrusive_ptr with a nested classes
具体来说,我需要声明(根据我的理解)intrusive_ptr_{add_ref,release}
为我引用的类的朋友:
#include <boost/intrusive_ptr.hpp>
using boost::intrusive_ptr;
class Outer {
public:
//user-exposed interface goes here
protected:
class Inner {
public:
Inner():refct(0){}
virtual ~Inner(){}
//machinery goes here
size_t refct;
};
friend void boost::intrusive_ptr_release(Inner *p);
friend void boost::intrusive_ptr_add_ref(Inner *p);
intrusive_ptr<Inner> handle;
};
namespace boost {
void intrusive_ptr_release(Outer::Inner *p){
if ((p->refct -= 1) <= 0){
delete p;
}
}
void intrusive_ptr_add_ref(Outer::Inner *p){
p->refct++;
}
};
我在找到正确的语法来进行编译并保持我想要的访问时遇到了麻烦。我的主要问题是gcc似乎对"boost::intrusive_ptr_release(Outer::Inner *p)应该在命名空间boost中声明"感到不安。
我从这个例子中看到,intrusive_ptr帮助器是在命名空间boost内部向前声明的——但是我不能向前声明它们,因为在我的理解中,嵌套类(即:"内部"(这些函数所引用的)只能在它们的外部类中向前声明,这也是友元声明必须要去的地方。
C++大师们,什么是正确的方法来处理这个?您不必将它们放在namespace boost
中,您可以将它们放在与class Outer
相同的命名空间中,它们将通过参数依赖查找找到。
每个新的intrusive_ptr实例使用不限定的调用intrusive_ptr_add_ref函数,并将指针作为参数传递给它,从而增加引用计数。
相关文章:
- 嵌套在类中时无法设置成员数据
- 无法访问嵌套类.类的使用无效
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- 如何在C++中初始化嵌套类中的2个memeber
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- 在C++中搜索嵌套多映射值
- 在C++中将矢量转换为嵌套地图
- C++嵌套if语句,基本货币交换
- 在nlohmann json中,如何将嵌套对象的数组转换为嵌套结构的向量
- 嵌套的匿名命名空间
- 了解嵌套循环打印星号图案
- 如何使用boost::具有嵌套结构和最小代码更改的序列化
- 嵌套for循环C++的问题(初学者)
- 从嵌套在std::映射中的std::列表中删除元素的最佳方式
- 用C#中的并集模拟C++嵌套结构
- 部分专业化和嵌套模板
- 嵌套While循环不起作用(C++问题)
- C++-模板嵌套类的引用初始化无效
- 如何在嵌套类中正确使用友元声明?
- UE4 C++ 我无法将 JSON 嵌套值获取到 TArray