Last active
August 29, 2015 14:26
-
-
Save kirillkovalenko/8219e7c1afea9b5da5da to your computer and use it in GitHub Desktop.
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
// 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