C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c – 这里发生了什么?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
这不编译,

#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);
}

但这样做:

#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);
}

还有这个 :

#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);
}

我想这与SFINAE有关(我还没有想过要理解)? friend限定符是否将已定义的函数作为自由函数放在封闭的命名空间中?

编辑

删除了他们的帖子,成员函数非朋友作为add_ref和release(documention中没有提到这些特定的成员函数……)确实解决了问题.使用好友限定符的嵌套定义会发生什么?

解决方法

从boost :: intrusive_ptr的文档:

@H_@L_673_8@_39@

Every new intrusive_ptr instance increments the reference count by using an unqualified call to the function intrusive_ptr_add_ref,passing it the pointer as an argument. Similarly,when an intrusive_ptr is destroyed,it calls intrusive_ptr_release; this function is responsible for destroying the object when its reference count drops to zero. The user is expected to provide suitable deFinitions of these two functions. On compilers that support argument-dependent lookup,intrusive_ptr_add_ref and intrusive_ptr_release should be defined in the namespace that corresponds to their parameter; otherwise,the deFinitions need to go in namespace boost.

这意味着intrusive_ptr_add_ref和intrusive_ptr_release不应该是成员函数,而是自由函数(友元函数就像这样).此外,它们在没有限定条件的情况下被调用,因此它们应该位于全局命名空间或ADL找到的某个位置.

编辑:关于使用朋友限定符的嵌套定义的问题:友元函数被定义为非成员函数,因此朋友void intrusive_ptr_add_ref(X * blah)将被称为intrusive_ptr_add_ref(my_x_ptr)而不是my_x_ptr-> intrusive_ptr_add_ref().

大佬总结

以上是大佬教程为你收集整理的c – 这里发生了什么?全部内容,希望文章能够帮你解决c – 这里发生了什么?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。