Last active
May 31, 2017 22:59
-
-
Save oliora/3f70b203bf063c6d75b7e559b4dcf09b to your computer and use it in GitHub Desktop.
Delegating deleter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///////////////////////////////////////////// | |
// parser.h | |
#include <memory> | |
template<class T> | |
struct delegating_delete { | |
inline void operator() (T *p) const noexcept { do_delete(p); } | |
}; | |
namespace parser { | |
class Parser { | |
public: | |
// ... | |
private: | |
class Impl; | |
friend void do_delete(Impl *p) noexcept; | |
std::unique_ptr<Impl, delegating_delete<Impl>> impl_; // PIMPL | |
}; | |
} | |
///////////////////////////////////////////// | |
// parser.cpp | |
#include "parser.h" | |
namespace parser { | |
class Parser::Impl { ... }; | |
void do_delete(Parser::Impl *p) noexcept { delete p; } | |
// Parser implementation... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment