Difference between revisions of "cpp/memory/unique ptr/get deleter"
From cppreference.com
< cpp | memory | unique ptr
m (Text replace - "/sidebar" to "/navbar") |
Florin.tene (Talk | contribs) (Added example....) |
||
Line 24: | Line 24: | ||
{{example | | {{example | | ||
| code= | | code= | ||
+ | #include <iostream> | ||
+ | #include <memory> | ||
+ | |||
+ | struct Foo { | ||
+ | Foo() { std::cout << "Foo...\n"; } | ||
+ | ~Foo() { std::cout << "~Foo...\n\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(); | ||
+ | } | ||
| output= | | output= | ||
+ | Foo... | ||
+ | Call deleter D::bar()... | ||
+ | Call delete for Foo object... | ||
+ | ~Foo... | ||
}} | }} |
Revision as of 14:44, 5 October 2012
Template:ddcl list begin <tr class="t-dcl ">
<td > Deleter& get_deleter();
</td>
<td class="t-dcl-nopad"> </td> <td > (since C++11) </td> </tr> <tr class="t-dcl ">
<td >const Deleter& get_deleter() const;
</td>
<td class="t-dcl-nopad"> </td> <td > (since C++11) </td> </tr> Template:ddcl list end
Returns the deleter object which would be used for destruction of the managed object.
Contents |
Parameters
(none)
Return value
The stored deleter object.
Exceptions
noexcept specification:
noexcept
Example
Run this code
#include <iostream> #include <memory> struct Foo { Foo() { std::cout << "Foo...\n"; } ~Foo() { std::cout << "~Foo...\n\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(); }
Output:
Foo... Call deleter D::bar()... Call delete for Foo object... ~Foo...