Bind two arguments

I have the following struct:

struct IDirect3D
{
    IDirect3D() : ref_count_(0) {}
    unsigned long Release() { return --ref_count_; }
private:
    long ref_count_;
    ~IDirect3D() {}
};

I want to use shared_ptrfor it, as in the following code (minimal example):

int main()
{
    boost::shared_ptr<IDirect3D> ptr;

    IDirect3D* p = 0; // initialized somewhere
    ptr.reset( p, boost::mem_fn( &IDirect3D::Release ) );

    return 0;
}

In most cases, this works fine, but at the same time, if pequal 0. I have the following deleter that I want to use:

template<typename T, typename D>
inline void SafeDeleter( T*& p, D d ) 
{ 
    if ( p != NULL ) {
        (p->*d)();
        p = NULL;
    }
}

But the following code gives a lot of errors (it looks like it unloads everything bind.hpp):

ptr.reset( p, boost::bind( SafeDeleter, _1, &IDirect3D::Release ) );

What happened to my use bind?

+3
source share
1 answer

Release()comes from IUnknown- so why not just use this:

void my_deleter(IUnknown* p) {
    // ...
}

ptr.reset(p, &my_deleter);

, Boost intrusive_ptr, :

void intrusive_ptr_add_ref(IUnknown* p) { p->AddRef (); }
void intrusive_ptr_release(IUnknown* p) { p->Release(); }

boost::intrusive_ptr<IDirect3D> d3d(...);
IDirect3D* p = 0;
d3d.reset(p);

, , , SafeDeleter - -, , :

ptr.reset(p, boost::bind(&SafeDeleter<IDirect3D, ULONG (IDirect3D::*)()>, 
                         _1, &IDirect3D::Release));
+5

Source: https://habr.com/ru/post/1759269/


All Articles