Skip to content

Instantly share code, notes, and snippets.

@oliora
Last active May 31, 2017 22:59
Show Gist options
  • Save oliora/3f70b203bf063c6d75b7e559b4dcf09b to your computer and use it in GitHub Desktop.
Save oliora/3f70b203bf063c6d75b7e559b4dcf09b to your computer and use it in GitHub Desktop.
Delegating deleter
/////////////////////////////////////////////
// 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