Namespaces
Variants

std:: get_deleter

From cppreference.net
Memory management library
( exposition only* )
Allocators
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Memory resources
Uninitialized storage (until C++20)
( until C++20* )
( until C++20* )
( until C++20* )

Garbage collector support (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
헤더에 정의됨 <memory>
template < class Deleter, class T >
Deleter * get_deleter ( const std:: shared_ptr < T > & p ) noexcept ;
(C++11 이후)

p 의 삭제자(deleter)에 접근합니다. 만약 공유 포인터 p 가 cv-unqualified Deleter 타입의 삭제자를 소유하고 있다면(예: 삭제자를 매개변수로 받는 생성자들 중 하나로 생성된 경우), 삭제자에 대한 포인터를 반환합니다. 그렇지 않으면 널 포인터를 반환합니다.

목차

매개변수

p - 삭제자를 접근해야 하는 공유 포인터

반환값

소유된 삭제자에 대한 포인터 또는 nullptr . 반환된 포인터는 적어도 하나의 shared_ptr 인스턴스가 이를 소유하는 동안 최소한 그 기간 동안 유효합니다.

참고 사항

반환된 포인터는 마지막 shared_ptr 보다 더 오래 생존할 수 있습니다. 예를 들어, std::weak_ptr 이 남아 있고 구현이 전체 제어 블록이 파괴될 때까지 삭제자를 파괴하지 않는 경우가 있습니다.

예제

std::shared_ptr 의 삭제자가 shared_ptr 의 타입과 독립적임을 보여줍니다.

#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!

참고 항목

std::shared_ptr 생성자들
(public member function)
관리 객체의 소멸에 사용되는 삭제자를 반환함
( std::unique_ptr<T,Deleter> 의 public member function)