New C++17 backport: folly::enable_shared_from_this
authorPhilipp Unterbrunner <philippu@fb.com>
Wed, 1 Mar 2017 06:14:32 +0000 (22:14 -0800)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Wed, 1 Mar 2017 06:20:21 +0000 (22:20 -0800)
commit512aa17590988db15d6f303528a45dfdcb9503de
tree8e3a2cd743d860591daed8fb5cfb3a18de49c3ab
parentc180b7021249a36750983fb8ebf3f26443d274a7
New C++17 backport: folly::enable_shared_from_this

Summary:
C++14 has no direct means of creating a std::weak_ptr, one must always create a (temporary) std::shared_ptr first. C++17 adds weak_from_this() to std::enable_shared_from_this to avoid that overhead.
Alas code that is meant to compile under different language versions cannot rely on std::enable_shared_from_this::weak_from_this().
This new utility class uses SFINAE to call std::enable_shared_from_this<T>::weak_from_this() if available. Falls back to std::enable_shared_from_this<T>::shared_from_this() otherwise.
Use as a drop-in replacement for std::enable_shared_from_this where weak_from_this() is desired.

Reviewed By: yfeldblum

Differential Revision: D4616394

fbshipit-source-id: 73bf6cd8852d4a33478a9280cc69c81e7ea73423
folly/Memory.h
folly/test/MemoryTest.cpp