Skip to content

Instantly share code, notes, and snippets.

@kirillkovalenko
Last active August 29, 2015 14:26
Show Gist options
  • Save kirillkovalenko/8219e7c1afea9b5da5da to your computer and use it in GitHub Desktop.
Save kirillkovalenko/8219e7c1afea9b5da5da to your computer and use it in GitHub Desktop.
// http://stackoverflow.com/questions/31651925/using-unique-ptr-shared-ptr-with-api-functions-returning-resources-as-out-para
template<typename P, typename T = typename P::element_type>
class outptr
{
T* _p; P& _sp;
public:
outptr(P& sp) throw() : _p(nullptr), _sp(sp) { }
~outptr() throw() { _sp.reset(_p); }
T** operator&() throw() { return &_p; }
};
template<typename T>
inline outptr<std::shared_ptr<T>> out(std::shared_ptr<T>& p)
{
return outptr<std::shared_ptr<T>>(p);
}
template<typename T, typename D>
inline outptr<std::unique_ptr<T, D>> out(std::unique_ptr<T, D>& p)
{
return outptr<std::unique_ptr<T, D>>(p);
}
// template<typename P>
// inline outptr<P> out(P& p)
// {
// return outptr<P>(p);
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment