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

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

问题描述

来自问题:

正确使用这个->

答案表明 -> 可以使用

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.

推荐答案

发表于其他问题:

template <class T>
struct foo : T {
  void bar() {
    x = 5;       // doesn't work
    this->x = 5; // works - T has a member named x
  }
};

没有 this-> 编译器不知道 x 是(继承的)成员.

Without this-> compiler doesn't know x is a (inherited) member.

类似于在模板代码中使用typenametemplate:

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天全站免登陆