Has*_*yed 3 c++ smart-pointers boost-smart-ptr
这不编译,
#include <boost/intrusive_ptr.hpp>
class X
{
public:
void intrusive_ptr_add_ref(X* blah)
{
}
void intrusive_ptr_release(X * blah)
{
}
};
int main()
{
boost::intrusive_ptr<X> ex(new X);
}
Run Code Online (Sandbox Code Playgroud)
但这样做:
#include <boost/intrusive_ptr.hpp>
class X
{
public:
friend void intrusive_ptr_add_ref(X* blah)
{
}
friend void intrusive_ptr_release(X * blah)
{
}
};
int main()
{
boost::intrusive_ptr<X> ex(new X);
}
Run Code Online (Sandbox Code Playgroud)
还有这个 :
#include <boost/intrusive_ptr.hpp>
class X
{
public:
};
void intrusive_ptr_add_ref(X* blah)
{
}
void intrusive_ptr_release(X * blah)
{
}
int main()
{
boost::intrusive_ptr<X> ex(new X);
}
Run Code Online (Sandbox Code Playgroud)
我想这与SFINAE有关(我还没有想过要理解)?friend限定符是否将已定义的函数作为自由函数放在封闭的命名空间中?
编辑
谁删除了他们的帖子,成员函数非朋友add_ref和release(这些特定成员函数未在文档中提及...)确实解决了问题.使用friend限定符的嵌套定义会发生什么?
来自以下文件boost::intrusive_ptr:
每个新的intrusive_ptr实例通过使用对函数intrusive_ptr_add_ref的非限定调用来递增引用计数,并将指针作为参数传递给它.类似地,当一个intrusive_ptr被销毁时,它会调用intrusive_ptr_release; 当函数的引用计数降为零时,此函数负责销毁对象.期望用户提供这两个功能的合适定义.在支持依赖于参数的查找的编译器上,应该在与其参数对应的命名空间中定义intrusive_ptr_add_ref和intrusive_ptr_release; 否则,定义需要进入名称空间提升.
这意味着intrusive_ptr_add_ref并且intrusive_ptr_release不应该是成员函数,而是自由函数(朋友函数表现如此).此外,它们在没有限定条件的情况下被调用,因此它们应该位于全局命名空间或ADL找到的某个位置.
编辑:关于使用friend限定符的嵌套定义的问题:
friend函数被定义为非成员函数,因此friend void intrusive_ptr_add_ref(X* blah)将被调用intrusive_ptr_add_ref(my_x_ptr)而不是作为my_x_ptr->intrusive_ptr_add_ref().
| 归档时间: |
|
| 查看次数: |
1548 次 |
| 最近记录: |