开发者

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

开发者 https://www.devze.com 2023-02-22 00:06 出处:网络
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 fairly detailed explanation?

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 fairly detailed explanation? Thanks!

EDIT: Here's an example

template< typename T, size_t N>
struct Vector {
public:
   Vector() {
       this->template operator=(0);
   }

   // ...       

   template< typename U >
   typename boost::enable_if< boost::is_convertible< U, T >, Vector& >::type operator=(Vector< U, N > const & other) {
       typename Vector< U, N >::ConstIterator j = other.begin();
       for (Iterator i = begin(); i != end(); ++i, ++j)
           (*i) = (*j);
       return *th开发者_高级运维is;
   } 
};

This example is from the ndarray project on Google Code and is not my own code.


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).


It used to disambiguation, and

// maybe: (handle->appendArray < 13) > (myarray);
handle->appendArray<13>(myarray);

Maybe some compilers can deduce it automatically.


When extending a class that depends on a template parameter, this kind of become a dependent name.

The problem is that while performing two phase name lookup, the compiler can't know where he can find the function hello. He cannot know it comes from the parent. Because template specialization is a thing, Base<int> and Base <double> could be two completely different clas swith different functions and members.

With the this keyword added, the compiler know that hello must be a member function.

Without that, it could be either a member function or non-member function.

#include <iostream>

template <class T>
class Base {
public:

    void hello() {std::cout << "hello\n";}
    
    void hello1() {std::cout << "hello1\n";}

    template <unsigned N>
    void hello2() {std::cout << "hello2<" << N << ">()\n";}
    
};

template <class T>
class Drivered: public Base<T> {
public:
    
    using Base<T>::hello;

    void test()
    {
        hello();
        this->hello1();
        this->template hello2<200>();
 }
};
int main() {
    Drivered<int> d;
    d.test();
}

【1】https://stackoverflow.com/a/39667832/4268594

0

精彩评论

暂无评论...
验证码 换一张
取 消