gpt4 book ai didi

c++11 - 为特定类型的 shared_ptr 创建默认删除器

转载 作者:行者123 更新时间:2023-12-02 03:25:05 25 4
gpt4 key购买 nike

有没有类似Well, how does the custom deleter of std::unique_ptr work?的东西对于 shared_ptr

当我尝试为指向 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,但我想知道是否有一种方法可以创建默认删除器,这样我就不必每次都传递相同的函数我创建了一个新的位图。

谢谢。

最佳答案

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

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

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

关于c++11 - 为特定类型的 shared_ptr 创建默认删除器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30834257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com