调用“this->template [somename]”是什么意思?做?

发布于 2024-10-30 01:13:41 字数 736 浏览 1 评论 0原文

我搜索过这个问题,但找不到任何相关内容。有没有更好的方法在 Google 中查询类似的内容,或者任何人都可以提供一个或多个链接或相当详细的解释吗?谢谢!

编辑:这是一个示例

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

此示例来自 Google 代码上的 ndarray 项目,不是我自己的代码。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尛丟丟 2024-11-06 01:13:41

下面是一个需要 this->template 的示例。但它与OP的示例并不真正匹配:

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

在此示例中,需要 this ,否则不会在基类中查找 alloc 因为基类是依赖的在模板参数 T 上。需要模板,因为否则“<”其目的是打开包含 200 的模板参数列表,否则将指示小于号 ([temp.names]/4)。

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

靖瑶 2024-11-06 01:13:41

当扩展依赖于模板参数的类时,这种类型将成为依赖名称

问题是,在执行两阶段名称查找时,编译器无法知道在哪里可以找到函数hello。他不知道它来自父母。因为模板专门化是一件事,所以 BaseBase 可能是两个完全不同的类,具有不同的函数和成员。

添加 this 关键字后,编译器知道 hello 必须是一个成员函数。

如果没有它,它可能是成员函数非成员函数

#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

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 classes with 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

夜访吸血鬼 2024-11-06 01:13:41

它用于消除歧义,

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

也许某些编译器可以自动推断它。

It used to disambiguation, and

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

Maybe some compilers can deduce it automatically.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文