Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 *this;
   } 
};

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
171 views
Welcome To Ask or Share your Answers For Others

1 Answer

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 << ">()
";}
};

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...