在线文档教程
C++
应用 | Utilities

std::get_deleter

STD:得到[医]德莱特

template< class Deleter, class T > Deleter* get_deleter( const std::shared_ptr& p (since C++11)

访问p%27s缺失。如果共享指针p拥有cv-不合格类型的删除器。Deleter%28等。如果它是用一个以删除器作为参数%29的构造函数创建的,那么返回一个指向删除器的指针。否则,返回空指针。

参数

p-a shared pointer whose deleter needs to be accessed

返回值

指向所拥有的删除项的指针或nullptr返回的指针至少在至少一个仍然有效。shared_ptr比如拥有它的人。

例外

noexcept规格:

noexcept

注记

返回的指针可能比最后一个指针更长。shared_ptr例如,如果std::weak_ptrS保持不变,实现%27T销毁删除器,直到整个控制块被销毁。

演示共享[医]ptr删除项独立于共享[医]PTR%27S型。

二次

#include <iostream> #include <memory> struct Foo { int i; }; void foo_deleter(Foo * p) { std::cout << "foo_deleter called!\n"; delete p; } int main() { std::shared_ptr<int> aptr; { // create a shared_ptr that owns a Foo and a deleter auto foo_p = new Foo; std::shared_ptr<Foo> r(foo_p, foo_deleter aptr = std::shared_ptr<int>(r, &r->i // aliasing ctor // aptr is now pointing to an int, but managing the whole Foo } // r gets destroyed (deleter not called) // obtain pointer to the deleter: if(auto del_p = std::get_deleter<void(*)(Foo*)>(aptr)) { std::cout << "shared_ptr<int> owns a deleter\n"; if(*del_p == foo_deleter) std::cout << "...and it equals &foo_deleter\n"; } else std::cout << "The deleter of shared_ptr<int> is null!\n"; } // deleter called here

二次

产出:

二次

shared_ptr<int> owns a deleter ...and it equals &foo_deleter foo_deleter called!

二次

另见

(constructor)std::shared_ptr constructors (public member function)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/Memory/Shared[医]PTR/GET[医]德莱特