What does a call to 'this->template [somename]' do?

后端 未结 1 1492
陌清茗
陌清茗 2020-12-08 07:28

I\'ve searched for this question and I can\'t find anything on it. Is there a better way to query something like this in Google or can anyone provide a link or links or a fa

相关标签:
1条回答
  • 2020-12-08 08:02

    Here is an example where this->template is required. It doesn't really match the OP's example though:

    #include <iostream>
    
    template <class T>
    struct X
    {
        template <unsigned N>
            void alloc() {std::cout << "alloc<" << N << ">()\n";}
    };
    
    template <class T>
    struct Y
        : public X<T>
    {
        void test()
        {
            this->template alloc<200>();
        }
    };
    
    int main()
    {
        Y<int> y;
        y.test();
    }
    

    In this example the this is needed because otherwise alloc would not be looked up in the base class because the base class is dependent on the template parameter T. The template is needed because otherwise the "<" which is intended to open the template parameter list containing 200, would otherwise indicate a less-than sign ([temp.names]/4).

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