2015-06-14 25 views
3

是否有類似於Well, how does the custom deleter of std::unique_ptr work?shared_ptr s?爲特定類型創建shared_ptr的默認刪除程序

當我試圖創建智能指針默認刪除器來ALLEGRO_BITMAP指針

namespace std { 
    template<> 
    class default_delete <ALLEGRO_BITMAP> { 
    public: 
     void operator()(ALLEGRO_BITMAP* ptr) { 
      al_destroy_bitmap(ptr); 
     } 
    }; 
} 

編譯器就行了

shared_ptr<ALLEGRO_BITMAP> black(al_create_bitmap(groundWidth, TILESIZE)); 

我知道我可以通過吐出

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>   ] 

刪除者shared_ptr,但我想知道是否有一種創建默認刪除器的方式,所以我不必每次創建新位圖時都保持傳遞相同的功能。

謝謝。

+0

'shared_ptr'不使用'default_delete';只有'unique_ptr'。 'shared_ptr'默認情況下直接'刪除'。 –

回答

1

如果你可以強制執行公約總是通過std::make_unique分配它,那麼這將工作:

std::shared_ptr<ALLEGRO_BITMAP> bmp; 
... 
bmp = std::make_unique<ALLEGRO_BITMAP>(); 

的缺失者將與對象移動,因此default_delete將在對象銷燬被調用。不幸的是,編譯器不能執行這個約定;不是沒有一些自定義類型的幫助。

相關問題