Search code examples
c++c++11constructorsmart-pointersforward-declaration

Forward declaration with smart pointers and inherited constructor


According to this question it is possible to forward declare smart pointers if all constructors and destructors are not inline (requiring fully defined types then). When no destructor is provided, the compiler will declare one and provide an inline definition which then requires the type in the smart pointer to be fully known in the header. The same applies for default constructors.

However, I have found out that it also applies for inherited constructors as well and it is a bit confusing for me. Consider:

class Base
{
public:
    Base(); //defined in cpp
};

class SomeClass;

class Derived : public Base
{
    using Base::Base;
    ~Derived(); //defined in cpp

    std::unique_ptr<SomeClass> ptr;
};

This will not compile unless the Derived constructor is declared explicitly and defined only in the source file. Why? The Base constructor is not inline and as far as I know the using directive should cause "inheritance" of the constructors in similar fashion as other members. Or does the compiler interpret it as "declare for me the same constructors as in Base and define them inline"?


Solution

  • The last sentence is your answer. The compiler interprets using Base::Base; as

    "I want Derived to have constructors with the same set of signatures as Base has. Please define them for me."