C++11 为特定类型的共享ptr创建默认删除程序

C++11 为特定类型的共享ptr创建默认删除程序,c++11,shared-ptr,C++11,Shared Ptr,是否有类似于forshared\u ptrs的内容 当我尝试为指向ALLEGRO\u位图的智能指针创建默认删除程序时 namespace std { template<> class default_delete < ALLEGRO_BITMAP > { public: void operator()(ALLEGRO_BITMAP* ptr) { al_destroy_bitmap(ptr);

是否有类似于for
shared\u ptr
s的内容

当我尝试为指向
ALLEGRO\u位图的智能指针创建默认删除程序时

namespace std {
    template<>
    class default_delete < ALLEGRO_BITMAP > {
    public:
        void operator()(ALLEGRO_BITMAP* ptr) {
            al_destroy_bitmap(ptr);
        }
    };
}
名称空间std{
模板
类默认\u删除{
公众:
void运算符()(快板位图*ptr){
al_销毁_位图(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>          ]
1>c:\ProgramFiles(x86)\microsoft visual studio 12.0\vc\include\memory(696):警告C4150:删除指向不完整类型“ALLEGRO_位图”的指针;没有调用析构函数
1> c:\users\john\allegro\allegro-5.0.10-msvc-11.0\include\allegro5\bitmap.h(12):参见“allegro\u位图”的声明
1> c:\ProgramFiles(x86)\microsoft visual studio 12.0\vc\include\memory(482):请参阅正在编译的函数模板实例化“void std::shared\u ptr::\u Resetp(\u Ux*)”的参考
1> 与
1>          [
1> _Ux=快板\u位图
1>          ]
1> c:\ProgramFiles(x86)\microsoft visual studio 12.0\vc\include\memory(482):请参阅正在编译的函数模板实例化“void std::shared\u ptr::\u Resetp(\u Ux*)”的参考
1> 与
1>          [
1> _Ux=快板\u位图
1>          ]
1> c:\users\john\documents\visualstudio 2013\projects\blockstacking\blockstacking\systems.cpp(190):请参阅正在编译的函数模板实例化“std::shared_ptr::shared_ptr(_Ux*)”的参考
1> 与
1>          [
1> _Ux=快板\u位图
1>          ]
1> c:\users\john\documents\visualstudio 2013\projects\blockstacking\blockstacking\systems.cpp(190):请参阅正在编译的函数模板实例化“std::shared_ptr::shared_ptr(_Ux*)”的参考
1> 与
1>          [
1> _Ux=快板\u位图
1>          ]
1> c:\program files(x86)\microsoft visual studio 12.0\vc\include\memory(159):警告C4150:删除指向不完整类型“ALLEGRO_位图”的指针;没有调用析构函数
1> c:\users\john\allegro\allegro-5.0.10-msvc-11.0\include\allegro5\bitmap.h(12):参见“allegro\u位图”的声明
1> c:\ProgramFiles(x86)\microsoft visual studio 12.0\vc\include\memory(158):编译类模板成员函数“void std::_Ref_count::_Destroy(void)”时
1> 与
1>          [
1> _Ux=快板\u位图
1>          ]
1> c:\ProgramFiles(x86)\microsoft visual studio 12.0\vc\include\memory(694):请参阅正在编译的类模板实例化“std::\u Ref\u count”的参考
1> 与
1>          [
1> _Ux=快板\u位图
1>          ]
1> c:\ProgramFiles(x86)\microsoft visual studio 12.0\vc\include\memory(482):请参阅正在编译的函数模板实例化“void std::shared\u ptr::\u Resetp(\u Ux*)”的参考
1> 与
1>          [
1> _Ux=快板\u位图
1>          ]
1> c:\ProgramFiles(x86)\microsoft visual studio 12.0\vc\include\memory(482):请参阅正在编译的函数模板实例化“void std::shared\u ptr::\u Resetp(\u Ux*)”的参考
1> 与
1>          [
1> _Ux=快板\u位图
1>          ]
在线

shared_ptr<ALLEGRO_BITMAP> black(al_create_bitmap(groundWidth, TILESIZE));
shared_ptr black(al_create_位图(groundidth,TILESIZE));
我意识到我可以将删除程序传递给共享\u ptr
,但我想知道是否有办法创建默认删除程序,这样我就不必在每次创建新位图时都传递相同的函数


谢谢。

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

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

删除器将随对象一起移动,因此在对象销毁时将调用默认的_delete。不幸的是,编译器无法执行此约定;如果没有某些自定义类型的帮助,则不能使用。

shared\u ptr
不使用
default\u delete
;只有
unique\u ptr
是唯一的<代码>共享\u ptr默认情况下只执行直接
删除操作。