为`std :: shared_ptr`专门设置`std :: default_delete` [英] specialise `std::default_delete` for `std::shared_ptr`
问题描述
我有这样的想法:
namespace std {
template<>
class default_delete<IplImage> {
public:
void operator()(IplImage *ptr) const {
cvReleaseImage(&ptr);
}
};
};
typedef std::shared_ptr<IplImage> IplImageObj;
我真的没有找到太多的信息,不管是否支持我专门 default_delete
并且 shared_ptr
是否也默认使用 default_delete
。
I didn't really found much information whether it is supported that I specialise default_delete
and whether shared_ptr
also uses default_delete
by default.
它的工作原理与Clang 5.0.0一样。
It works like intended with Clang 5.0.0.
那么,是否支持?
如果STL实现有不同的内部命名空间怎么办?它不会找到我的声明?但是它应该关于声明的错误。
What if the STL implementation has a different internal namespace? It wouldn't find my declaration then? But it should error about the declaration then.
推荐答案
default_delete
应该在std命名空间中定义并且可以从std命名空间中专门化实体。
default_delete
should be defined in std namespace and it's ok to specialize entities from std namespace.
namespace std {
template<class T> struct default_delete;
template<class T> struct default_delete<T[]>;
$ b $ c>它是UB。有关此事的引语是此处
但是,没有指定 shared_ptr
应该使用 default_delete
。
〜shared_ptr();However, it's not specified that
shared_ptr
should usedefault_delete
.~shared_ptr();
效果:
- 如果*为空或与另一个shared_ptr实例(use_count()> 1)共享所有权,
没有副作用。— If *this is empty or shares ownership with another shared_ptr instance (use_count() > 1), there are no side effects.
如果*拥有一个对象p和一个删除器d,则调用d(p)。
— Otherwise, if *this owns an object p and a deleter d, d(p) is called.
- 否则,*拥有一个指针p,
— Otherwise, *this owns a pointer p, and delete p is called.
这篇关于为`std :: shared_ptr`专门设置`std :: default_delete`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!