为特定类型

时间:2015-06-14 20:33:48

标签: c++11 shared-ptr

shared_ptr是否有类似于Well, how does the custom deleter of std::unique_ptr work?的内容?

当我尝试为ALLEGRO_BITMAP指针

的智能指针创建默认删除器时
namespace std {
    template<>
    class default_delete < ALLEGRO_BITMAP > {
    public:
        void operator()(ALLEGRO_BITMAP* ptr) {
            al_destroy_bitmap(ptr);
        }
    };
}

编译器吐出

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<ALLEGRO_BITMAP> black(al_create_bitmap(groundWidth, TILESIZE));

我意识到我可以将删除器传递给shared_ptr,但我想知道是否有办法创建默认删除器,所以每次我都不必继续传递相同的功能创建一个新的位图。

感谢。

1 个答案:

答案 0 :(得分:1)

如果您可以强制执行约定以始终通过std::make_unique分配它,那么这将起作用:

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

删除器将随对象移动,因此将在对象销毁时调用default_delete。不幸的是,编译器无法执行此约定;没有一些自定义类型的帮助。