官方上,什么是typename? [英] Officially, what is typename for?

查看:172
本文介绍了官方上,什么是typename?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时,我看到一些真正无法解读的错误消息,gcc当使用模板时吐出...具体来说,我有一些问题,看起来正确的声明导致非常奇怪的编译错误,通过前缀typename 关键字到声明的开始...(例如,刚刚上周,我声明两个迭代器作为另一个模板类的成员,我不得不这样做)...

On occasion I've seen some really indecipherable error messages spit out by gcc when using templates... Specifically, I've had problems where seemingly correct declarations were causing very strange compile errors that magically went away by prefixing the "typename" keyword to the beginning of the declaration... (For example, just last week, I was declaring two iterators as members of another templated class and I had to do this)...

关于typename的故事是什么?

What's the story on typename?

推荐答案

以下是Josuttis书籍的报价:

Following is the quote from Josuttis book:


关键字typename被引入
指定
后面的标识符是一个类型。考虑
以下示例:

The keyword typename was introduced to specify that the identifier that follows is a type. Consider the following example:

template <class T>
Class MyClass
{
  typename T::SubType * ptr;
  ...
};

这里,typename用来说明
SubType是类T的类型。 ,
ptr是一个指向类型
T :: SubType的指针。如果没有typename,SubType
将被认为是静态成员。
因此

Here, typename is used to clarify that SubType is a type of class T. Thus, ptr is a pointer to the type T::SubType. Without typename, SubType would be considered a static member. Thus

T::SubType * ptr

将是值$ b的乘法$ b类型T的子类型与ptr。

would be a multiplication of value SubType of type T with ptr.

这篇关于官方上,什么是typename?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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