When I inherit from std::enable_shared_from_this
, but I create a unique_ptr, will the weak_ptr
inside std::enable_shared_from_this
also be initialized when I "move" to a shared_ptr
by std::move
, or by the move constructor?
For example what will happen in the below code:
#include <memory>
#include <iostream>
class A : public std::enable_shared_from_this< A >
{
public:
std::shared_ptr< A > getA()
{
return shared_from_this();
}
};
int main()
{
std::unique_ptr< A > u(new A());
// Aborts
//std::cout << u->getA() << std::endl;
std::shared_ptr< A > s(std::move(u));
// Will this work or abort too?
std::cout << s << ", " << s->getA() << std::endl;
}
According to https://en.cppreference.com/w/cpp/memory/enable_shared_from_this all the std::shared_ptr
constructors should initialise the internal weak reference so moving a std::unique_ptr
into a std::shared_ptr
should enable the use of shared_from_this
.
You do have to be careful that nobody calls shared_from_this
whilst the object is owned by std::unique_ptr
as that is either undefined behaviour or will throw std::bad_weak_ptr
depending on your c++ version.
[util.smartptr.shared.const]/1 In the constructor definitions below, enables
shared_from_this
withp
, for a pointerp
of typeY*
, means that ifY
has an unambiguous and accessible base class that is a specialization ofenable_shared_from_this
(23.11.2.5), thenremove_cv_t<Y>*
shall be implicitly convertible toT*
and the constructor evaluates the statement:if (p != nullptr && p->weak_this.expired()) p->weak_this = shared_ptr<remove_cv_t<Y>>(*this, const_cast<remove_cv_t<Y>*>(p));
template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
[util.smartptr.shared.const]/29 Effects: ... equivalent to
shared_ptr(r.release(), r.get_deleter())
template<class Y, class D> shared_ptr(Y* p, D d);
[util.smartptr.shared.const]/10 Effects: Constructs a
shared_ptr
object that owns the objectp
and the deleterd
. WhenT
is not an array type, ... enableshared_from_this
withp
So yes, std::shared_ptr< A > s(std::move(u));
does initialize things appropriately to make shared_from_this
work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With