Is there something similar to Well, how does the custom deleter of std::unique_ptr work? for shared_ptr
s?
When I try creating a default deleter for smart pointers to ALLEGRO_BITMAP
pointers
namespace std {
template<>
class default_delete < ALLEGRO_BITMAP > {
public:
void operator()(ALLEGRO_BITMAP* ptr) {
al_destroy_bitmap(ptr);
}
};
}
The compiler spits out
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\memory(696): warning C4150: deletion of pointer to incomplete type 'ALLEGRO_BITMAP'; no destructor called
1> c:\users\john\allegro\allegro-5.0.10-msvc-11.0\include\allegro5\bitmap.h(12) : see declaration of 'ALLEGRO_BITMAP'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\memory(482) : see reference to function template instantiation 'void std::shared_ptr<ALLEGRO_BITMAP>::_Resetp<_Ux>(_Ux *)' being compiled
1> with
1> [
1> _Ux=ALLEGRO_BITMAP
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\memory(482) : see reference to function template instantiation 'void std::shared_ptr<ALLEGRO_BITMAP>::_Resetp<_Ux>(_Ux *)' being compiled
1> with
1> [
1> _Ux=ALLEGRO_BITMAP
1> ]
1> c:\users\john\documents\visual studio 2013\projects\blockstacking\blockstacking\systems.cpp(190) : see reference to function template instantiation 'std::shared_ptr<ALLEGRO_BITMAP>::shared_ptr<ALLEGRO_BITMAP>(_Ux *)' being compiled
1> with
1> [
1> _Ux=ALLEGRO_BITMAP
1> ]
1> c:\users\john\documents\visual studio 2013\projects\blockstacking\blockstacking\systems.cpp(190) : see reference to function template instantiation 'std::shared_ptr<ALLEGRO_BITMAP>::shared_ptr<ALLEGRO_BITMAP>(_Ux *)' being compiled
1> with
1> [
1> _Ux=ALLEGRO_BITMAP
1> ]
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\memory(159): warning C4150: deletion of pointer to incomplete type 'ALLEGRO_BITMAP'; no destructor called
1> c:\users\john\allegro\allegro-5.0.10-msvc-11.0\include\allegro5\bitmap.h(12) : see declaration of 'ALLEGRO_BITMAP'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\memory(158) : while compiling class template member function 'void std::_Ref_count<_Ux>::_Destroy(void)'
1> with
1> [
1> _Ux=ALLEGRO_BITMAP
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\memory(694) : see reference to class template instantiation 'std::_Ref_count<_Ux>' being compiled
1> with
1> [
1> _Ux=ALLEGRO_BITMAP
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\memory(482) : see reference to function template instantiation 'void std::shared_ptr<ALLEGRO_BITMAP>::_Resetp<_Ux>(_Ux *)' being compiled
1> with
1> [
1> _Ux=ALLEGRO_BITMAP
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\memory(482) : see reference to function template instantiation 'void std::shared_ptr<ALLEGRO_BITMAP>::_Resetp<_Ux>(_Ux *)' being compiled
1> with
1> [
1> _Ux=ALLEGRO_BITMAP
1> ]
on the line
shared_ptr<ALLEGRO_BITMAP> black(al_create_bitmap(groundWidth, TILESIZE));
I realize I can pass the deleter to the shared_ptr
, but I was wondering if there was a way to create a default deleter so I don't have to keep passing in the same function every time I create a new bitmap.
Thanks.
If you can enforce a convention to always allocate it via
std::make_unique
then this will work:The deleter will move with the object, and therefore default_delete will be called upon object destruction. Unfortunately, the compiler can't enforce this convention; not without help from some custom types.