Why does C++ require me to repeat template arguments of my base class in initalizer list?

前端 未结 1 820
醉梦人生
醉梦人生 2021-01-21 02:34

I was porting some code from MSVC(without permissive-) to linux and I learned that if you call constructor of a template base class in initializer list of your class you must sp

相关标签:
1条回答
  • 2021-01-21 02:58

    You only need to do that when Derived is a template and the type of the base depends on its template parameters.

    This compiles, for example:

    template <typename T>
    class Derived : public Base<int, false>
    {
    public:
        Derived(const T& t) : Base(t) {}
    };
    

    As far as I know, here (in member initializer list) Base is actually the injected-class-name of Base<...>, inherited from it like everything else.

    And if the type of the base does depend on the template parameters, its inherited injected-class-name becomes inaccessible (at least directly), just like any other member inherited from it.

    For a member variable/function, you'd add this-> to access it, but for a type member you need Derived:::

    template <typename T>
    class Derived : public Base<T, false>
    {
    public:
        Derived(const T& t) : Derived::Base(t) {}
    };
    
    0 讨论(0)
提交回复
热议问题