调用“this->template [somename]”是什么意思?做?
我搜索过这个问题,但找不到任何相关内容。有没有更好的方法在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
下面是一个需要
this->template
的示例。但它与OP的示例并不真正匹配:在此示例中,需要
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:In this example the
this
is needed because otherwisealloc
would not be looked up in the base class because the base class is dependent on the template parameterT
. Thetemplate
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).当扩展依赖于模板参数的类时,这种类型将成为
依赖名称
。问题是,在执行
两阶段名称查找
时,编译器无法知道在哪里可以找到函数hello
。他不知道它来自父母。因为模板专门化是一件事,所以Base
和Base
可能是两个完全不同的类,具有不同的函数和成员。添加
this
关键字后,编译器知道hello
必须是一个成员函数。如果没有它,它可能是
成员函数
或非成员函数
。【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 functionhello
. He cannot know it comes from the parent. Because template specialization is a thing,Base<int>
andBase<double>
could be two completely different classes with different functions and members.With the
this
keyword added, the compiler know thathello
must be a member function.Without that, it could be either a
member function
ornon-member function
.【1】https://stackoverflow.com/a/39667832/4268594
它用于消除歧义,
也许某些编译器可以自动推断它。
It used to disambiguation, and
Maybe some compilers can deduce it automatically.