使用 - >在模板中,以强制以下符号依赖 [英] Use of -> in a template in order to force the following symbol to be dependent

查看:115
本文介绍了使用 - >在模板中,以强制以下符号依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从问题:



正确使用 - >



答案说明可以使用 - >


...在模板中,为了强制以下符号为
从属 - 在后面的使用,它通常是不可避免的。


这是什么意思,什么是这个用法的好例子?我不太明白依赖是什么意思,但它听起来像一个有用的技巧。

解决方案

问题:

 模板< 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:

Proper use of this->

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.

这篇关于使用 - &gt;在模板中,以强制以下符号依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆