Search code examples
c++inheritancetypedefusing-directivesusing-declaration

Why can't a typedef type be used to declare its parent class' ctors?


template<typename>
struct A
{
    int n;

    A(bool)
    {}
};

template<typename>
struct B
{
    struct C : A<B>
    {
        using Base = A<B>;

        using A<B>::A; // ok
        using Base::n; // ok

        // error: dependent using declaration resolved to type without 'typename'
        using Base::A;
    };

    C get() const
    {
        return C(true);
    }
};

int main()
{
    auto b = B<int>();
    b.get();
}

The error is described in the code.

Why can't a typedef type be used to declare its parent class' ctors?


Solution

  • Similar behaviour was earlier reported as a possible Clang bug: [Bug 23107] Constructor inheritance on template not working correctly.

    Richard Smith's comment on this report:

    The C++ committee have discussed this case and did not intend for that syntax to be valid. Use using myBase::myBase; instead to declare an inheriting constructor.

    So, you should write using Base::Base; instead of using Base::A;. After this fix, your code compiles with Clang.