5

I have two classes A and B, where B is a subclass of A. I need both classes to use std::enable_shared_from_this.

I tried this:

#include <memory>
#include <iostream>
#include <vector>


class A : public std::enable_shared_from_this<A> {
  public:
    void insertme(std::vector<std::shared_ptr<A>>& v) {
        std::cout << "A::insertme\n";
        v.push_back(shared_from_this());
        std::cout << "OK\n";
    }
};

class B : public A, public std::enable_shared_from_this<B> {
  public:
    void insertme(std::vector<std::shared_ptr<B>>& v) {
        std::cout << "B::insertme\n";
        v.push_back(std::enable_shared_from_this<B>::shared_from_this());
        std::cout << "OK\n";
    }
};

int main()
{
    std::vector<std::shared_ptr<A>> va;
    std::vector<std::shared_ptr<B>> vb;

    std::shared_ptr<A> pa = std::make_shared<A>();
    std::shared_ptr<B> pb = std::make_shared<B>();

    pa->insertme(va);
    pb->insertme(vb);
}

(In order to avoid that shared_from_this() be ambiguous, I had to fully qualify it in B::insertme.)

When I run the above program, I get this output:

A::insertme
OK
B::insertme
terminate called after throwing an instance of 'std::bad_weak_ptr'
  what():  bad_weak_ptr
Aborted (core dumped)

So A::insertme works, but B::insertme does not.

I'm using GCC 9.1.0 under Linux.

What am I doing wrong?

curiousguy
  • 8,038
  • 2
  • 40
  • 58
oz1cz
  • 5,504
  • 6
  • 38
  • 58

2 Answers2

7

You only need to (and only can) inherit from shared_from_this in the base class:

class A : public std::enable_shared_from_this<A> {
  public:
    void insertme(std::vector<std::shared_ptr<A>>& v) {
        std::cout << "A::insertme\n";
        v.push_back(shared_from_this());
        std::cout << "OK\n";
    }
};

class B : public A {
  public:
    void insertme(std::vector<std::shared_ptr<B>>& v) {
        std::cout << "B::insertme\n";
        v.push_back(std::static_pointer_cast<B>(shared_from_this()));
        std::cout << "OK\n";
    }
};

This means you need the explicit static_pointer_cast to get a shared_ptr<B>, but you could wrap that into an override in B if you want:

std::shared_ptr<B> shared_from_this() { return std::static_pointer_cast<B>(A::shared_from_this()); }
Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
  • 1
    Thank you. I've accepted your answer, but with the caveat that I cannot find `static_pointer_cast` as a member function of `std::shared_ptr`. It does, however, exist as a non-member function: `std::static_pointer_cast(A::shared_from_this())` – oz1cz Mar 18 '20 at 09:36
1

The automatic linkage to enable_shared_from_this<X> that gets set up when a shared_ptr<T> is created only works if the class type T inherits exactly one unambiguous public enable_shared_from_this base. But B inherits two different enable_shared_from_this bases.

Instead, you can have just the enable_shared_from_this<A>, and write a custom B::shared_from_this() which makes use of A::shared_from_this():

class B : public A {
  public:
    // These hide the A::shared_from_this(), but they can still be
    // used by qualification if wanted.
    std::shared_ptr<B> shared_from_this()
    { return std::static_pointer_cast<B>(A::shared_from_this()); }
    std::shared_ptr<const B> shared_from_this() const
    { return std::static_pointer_cast<B>(A::shared_from_this()); }

    // ...
};
aschepler
  • 70,891
  • 9
  • 107
  • 161
  • "_only works if the class type T inherits exactly one unambiguous public `enable_shared_from_this` base_" note that here all base classes are public and unambiguous – curiousguy Mar 29 '20 at 18:32
  • True. Just covering the "bases" more generally. – aschepler Mar 29 '20 at 22:11