gpt4 book ai didi

c++ - 调用 'this->template [somename]' 有什么作用?

转载 作者:IT老高 更新时间:2023-10-28 21:38:51 26 4
gpt4 key购买 nike

我已经搜索过这个问题,但找不到任何内容。有没有更好的方法在 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;
}
};

这个例子来自 ndarray Google Code 上的项目,不是我自己的代码。

最佳答案

这是一个需要 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 template 是必需的,否则用于打开包含 200 的模板参数列表的“<”将指示小于号 ([temp.names]/4)。

关于c++ - 调用 'this->template [somename]' 有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5533354/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com