Creating a default deducer for shared_ptr for a specific type
Is there something similar to OK, how does custom delete std :: unique_ptr work? for shared_ptr
s?
When I try to create a default 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 deleter to shared_ptr
, but I was wondering if there is a way to create a default so I don't have to keep passing the same function every time I create a new bitmap.
Thank.
source to share
If you can force the convention to always highlight it through std::make_unique
, then this will work:
std::shared_ptr<ALLEGRO_BITMAP> bmp;
...
bmp = std::make_unique<ALLEGRO_BITMAP>();
The delegator will move along with the object, and therefore default_delete will be called by destroying the object. Unfortunately, the compiler cannot enforce this convention; not without the help of some custom types.
source to share