Skip to content

Instantly share code, notes, and snippets.

@gintenlabo
Created August 15, 2013 11:45
Show Gist options
  • Save gintenlabo/6240205 to your computer and use it in GitHub Desktop.
Save gintenlabo/6240205 to your computer and use it in GitHub Desktop.
get two resources with exceptional safety
template<class T, class D>
auto make_unique_ptr(T* p, D d) noexcept
-> std::unique_ptr<T, D> {
return std::unique_ptr<T, D>(p, std::move(d));
}
auto get_two_resources()
-> std::pair<std::shared_ptr<Res1>, std::shared_ptr<Res2>> {
Res1* res1_ = {};
Res2* res2_ = {};
get_resources(&res1_, &res2_);
auto res1 = make_unique_ptr(res1_, [](Res1* p){ release(res1); });
auto res2 = make_unique_ptr(res2_, [](Res2* p){ release(res2); });
auto pair = std::make_pair(std::move(res1), std::move(res2));
auto p = std::make_shared<decltype(pair)>(std::move(pair));
return std::make_pair(
std::shared_ptr<Res1>(p, p->first.get()),
std::shared_ptr<Res2>(p, p->second.get()));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment