C++ - 结合 typedef 和 typename 的语句的含义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18385418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ - meaning of a statement combining typedef and typename
提问by Arvind
In a C++ header file, I am seeing this code:
在 C++ 头文件中,我看到了以下代码:
typedef typename _Mybase::value_type value_type;
Now, as I understand, quoting from ? C++ the Complete Reference? by Schildt. typename
can be substituted by keyword class, the second use of typename
is to inform the compiler that a name used in a template declaration is a type name rather than an object name.
现在,据我所知,引用自 ? C++ 完整参考?通过希尔特。typename
可以用关键字 class 代替,第二个用途typename
是通知编译器在模板声明中使用的名称是类型名称而不是对象名称。
Similarly, you can define new data type names by using the keyword typedef
. You are not
actually creating a new data type, but rather defining a new name for an existing
type.
同样,您可以使用关键字定义新的数据类型名称typedef
。您实际上并不是在创建新数据类型,而是为现有类型定义新名称。
However, can you explain exactly what is the meaning of the above line of code, where typedef
and typename
are combined together. And what does the "::
" in the statement imply?
不过,你能不能解释一下上面这行代码到底是什么意思,在哪里typedef
和typename
组合在一起。::
声明中的“ ”是什么意思?
回答by pippin1289
typedef is defining a new type for use in your code, like a shorthand.
typedef 正在定义一种新类型以在您的代码中使用,就像速记一样。
typedef typename _MyBase::value_type value_type;
value_type v;
//use v
typename here is letting the compiler know that value_type
is a type and not a static member of _MyBase
.
这里的 typename 是让编译器知道这value_type
是一个类型而不是_MyBase
.
the ::
is the scope of the type. It is kind of like "is in" so value_type
"is in" _MyBase
. or can also be thought of as contains.
的::
是该类型的范围。它有点像 "is in" 所以value_type
"is in" _MyBase
。或者也可以被认为是包含。
回答by Paul Evans
the typename
is saying that _Mybase::value_type
is the name of type so the typedef
can reley on that fact.
该typename
是说,_Mybase::value_type
是类型的名称,以便typedef
可以对事实reley。