Officially, what is typename for?(正式地说,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 有什么故事?
推荐答案
以下是 Josuttis 书中的引述:
Following is the quote from Josuttis book:
关键字typename被引入指定标识符下面是一个类型.考虑下面的例子:
The keyword
typenamewas 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 是class 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
将是价值的倍增SubType 类型为 T 和 ptr.
would be a multiplication of value
SubType of type T with ptr.
这篇关于正式地说,typename 是做什么用的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:正式地说,typename 是做什么用的?
基础教程推荐
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- CString 到 char* 2021-01-01
