Why can't my derived class find my base class's type alias?

后端 未结 1 891
野趣味
野趣味 2021-01-20 03:56

Let\'s start with the code example, because it should be easy to see what\'s going on:

template 
struct Base
{
    using Type = int;
};

te         


        
相关标签:
1条回答
  • 2021-01-20 04:19

    because Type is a dependent name (it depends on the template Base<T>). You need to qualify it and use typename. You can also qualify it with Derived:: (as the op himself figure it out):

    template<typename T>
    struct Derived : public Base<T>
    {
        using NewType = typename Base<T>::Type;
        // or
        using NewType2 = typename Derived::Type;
    };
    

    You can read more about dependent names here:

    https://en.cppreference.com/w/cpp/language/dependent_name

    How do you understand dependent names in C++

    Where and why do I have to put the "template" and "typename" keywords?

    Why do I have to access template base class members through the this pointer?

    "not declared in this scope" error with templates and inheritance

    0 讨论(0)
提交回复
热议问题