Przejdź do głównej zawartości

std::unique_ptr<T,Deleter>::get_deleter

Since C++11
Deleter& get_deleter() noexcept;
const Deleter& get_deleter() const noexcept;
Since C++23
constexpr Deleter& get_deleter() noexcept;
constexpr const Deleter& get_deleter() const noexcept;

Returns the deleter object which would be used for destruction of the managed object.

Parameters

(none)

Return value

The stored deleter object.

Example

#include <iostream>
#include <memory>

struct Foo
{
Foo() { std::cout << "Foo...\n"; }
~Foo() { std::cout << "~Foo...\n"; }
};

struct D
{
void bar() { std::cout << "Call deleter D::bar()...\n"; }
void operator()(Foo* p) const
{
std::cout
<< "Call delete for Foo object...\n";
delete p;
}
};

int main()
{
std::unique_ptr<Foo, D> up(new Foo(), D());
D& del = up.get_deleter();
del.bar();
}
Result
Foo...
Call deleter D::bar()...
Call delete for Foo object...
~Foo...

std::unique_ptr<T,Deleter>::get_deleter

Since C++11
Deleter& get_deleter() noexcept;
const Deleter& get_deleter() const noexcept;
Since C++23
constexpr Deleter& get_deleter() noexcept;
constexpr const Deleter& get_deleter() const noexcept;

Returns the deleter object which would be used for destruction of the managed object.

Parameters

(none)

Return value

The stored deleter object.

Example

#include <iostream>
#include <memory>

struct Foo
{
Foo() { std::cout << "Foo...\n"; }
~Foo() { std::cout << "~Foo...\n"; }
};

struct D
{
void bar() { std::cout << "Call deleter D::bar()...\n"; }
void operator()(Foo* p) const
{
std::cout
<< "Call delete for Foo object...\n";
delete p;
}
};

int main()
{
std::unique_ptr<Foo, D> up(new Foo(), D());
D& del = up.get_deleter();
del.bar();
}
Result
Foo...
Call deleter D::bar()...
Call delete for Foo object...
~Foo...