Using boost :: intrusive_ptr with nested classes

In particular, I need to declare (as I understand it) intrusive_ptr_{add_ref,release} as friends of my reference class:

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

I am having trouble finding the correct syntax to do this compilation and keep the access I want. My main problem is that gcc seems to be upset that "boost :: intrusive_ptr_release (Outer :: Inner * p) should have been declared in the namespace boostpace".

I can see from this example that the helpers of intrusive_ptr are declared forward in the namespace extension, but I cannot forward them, because as I understand it, nested classes (that is, the "Internal" to which these functions refer) can only be declared forward in their outer classes and that where the friendโ€™s announcement should appear.

O more C ++ gurus, what is the right way to handle this?

+4
source share
1 answer

You do not need to put them in namespace boost , you can place them in the same namespace as your class Outer , and they will be found depending on the search dependent on it.

Each new instance of intrusive_ptr increments the reference counter by using an unqualified call to the intrusive_ptr_add_ref function, passing it a pointer as an argument.

+5
source

All Articles