2015-06-14 92 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将在对象销毁被调用。不幸的是,编译器不能执行这个约定;不是没有一些自定义类型的帮助。

相关问题