C++ 将boost::intrusive_ptr与嵌套类一起使用
具体来说,我需要声明(据我所知)C++ 将boost::intrusive_ptr与嵌套类一起使用,c++,boost,namespaces,smart-pointers,nested-class,C++,Boost,Namespaces,Smart Pointers,Nested Class,具体来说,我需要声明(据我所知)intrusive\u ptr{add\u ref,release}为我引用类的朋友: #include <boost/intrusive_ptr.hpp> using boost::intrusive_ptr; class Outer { public: //user-exposed interface goes here protected: class Inner { public: Inner():
intrusive\u ptr{add\u 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++;
}
};
#包括
使用boost::intrusive_ptr;
类外部{
公众:
//用户公开界面在这里
受保护的:
班级内部{
公众:
内部():refct(0){}
虚~internal(){}
//这里有机器
尺寸反映;
};
friend void boost::侵入式释放(内部*p);
friend void boost::侵入性的\u ptr\u add\u ref(内部*p);
侵入式ptr手柄;
};
名称空间提升{
无效侵入性释放(外部::内部*p){
如果((p->refct-=1)refct++);
}
};
我很难找到正确的语法来编译并保留我想要的访问权限。我的主要问题是gcc似乎对“boost::intrusive_ptr_release(Outer::Inner*p)应该在命名空间boost中声明”感到不安
我从中看到,侵入性的_ptr helpers是在名称空间boost中进行前向声明的——但我不能进行前向声明,因为据我所知,嵌套类(即这些函数所指的“内部”)只能在其外部类中进行前向声明,这也是友元声明必须进行的地方
>大C++的Grurs,什么是正确的处理方法?
< P>你不必把它们放在<代码>命名空间Boosib>代码>中,你可以把它们放在与你的<代码>类外< /C>相同的命名空间中,它们将通过依赖于参数的查找找到。 每个新的intrusive_ptr实例都会通过使用unqualified调用函数intrusive_ptr_add_ref来增加引用计数,并将指针作为参数传递给它你说得对,这确实有效。我读过的一篇文档明确指出,friend函数必须位于boost名称空间(我链接的示例支持该名称空间),因此我认为这是理所当然的。不知道为什么我没有早点尝试。谢谢!这里的关键字是“依赖参数的查找”(通常称为凯尼格的查找或者仅仅是ADL)。在上面有一个谷歌,它是很有价值的,它是如何理解C++的。