使用 - >在模板中,以强制以下符号依赖 [英] Use of -> in a template in order to force the following symbol to be dependent
问题描述
从问题:
答案说明可以使用 - >
...在模板中,为了强制以下符号为
从属 - 在后面的使用,它通常是不可避免的。
这是什么意思,什么是这个用法的好例子?我不太明白依赖是什么意思,但它听起来像一个有用的技巧。
问题:
模板< class T>
struct foo:T {
void bar(){
x = 5; // does not work
this-> x = 5; // works - T has a member x
}
};
没有 this->
类似于使用
typename
x / code>和
模板
内部模板代码:
<类T,类S>
struct foo:T {
typedef T :: ttype< S> ;; // does not work
typedef typename T :: template ttype< S> footype; // works
};
这是愚蠢的,有点不必要,但你还是要这样做。
From the question:
The answer states that -> can be used
...in a template, in order to force the following symbol to be dependent—in this latter use, it is often unavoidable.
What does this mean what what would a good example of this use be? I don't quite what "dependent" means in this context, but it sounds like a useful trick.
Posted in other question:
template <class T>
struct foo : T {
void bar() {
x = 5; // doesn't work
this->x = 5; // works - T has a member named x
}
};
Without this->
compiler doesn't know x
is a (inherited) member.
Similar to use of typename
and template
inside template code:
template <class T, class S>
struct foo : T {
typedef T::ttype<S>; // doesn't work
typedef typename T::template ttype<S> footype; // works
};
It's silly and somewhat unnecessary, but you still gotta do it.
这篇关于使用 - >在模板中,以强制以下符号依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!