c++gcccompiler-errorsmake-shared

Error when casting derived class to protected base interface in std::make_shared


I have this code:

#include <memory>

class SomeInterface {
public:
    virtual void VirtualMethod() {}

    virtual void PureVirtualMethod() = 0;
};

class SomeInterfaceDependent {
public:
    explicit SomeInterfaceDependent(SomeInterface* a) : a_(a) {}

private:
    SomeInterface* const a_;
};

class Implementation : protected SomeInterface {
public:
    void Init() {
        // Ok
        auto raw = new SomeInterfaceDependent(this);

        // Cannot cast 'Implementation' to its protected base class 'SomeInterface'.
        auto shared = std::make_shared<SomeInterfaceDependent>(this);

        // Ok
        SomeInterface* casted_some_interface = this;
        auto shared2 = std::make_shared<SomeInterfaceDependent>(casted_some_interface);
    }

protected:
    void PureVirtualMethod() override {}
};


int main() {
    Implementation i;
    i.Init();

    return 0;
}

C++ standard 17, compiler GCC.

I get error error: ‘SomeInterface’ is an inaccessible base of ‘Implementation’ when (and):

Why? Is it the std::make_shared bug?


Solution

  • Is it the std::make_shared bug?

    No.

    Why?

    std::make_shared is not a friend of Implementation, and therefore it doesn't have access to its non-public bases, and therefore it cannot implicitly convert the pointer.