C ++ - value of a statement combining typedef and typename

In the C ++ header file, I see this code:

typedef typename _Mybase::value_type value_type; 

Now, as I understand it, a quote from Schildt’s “C ++ Complete Reference”. typename can be replaced by a class of keywords, the second use of typename is to tell the compiler that the name used in the template declaration is a type name, not an object name.

Similarly, you can define new data type names using the typedef keyword. You're not actually creating a new data type, but rather defining a new name for an existing type.

However, can you explain exactly what the above line of code means, where typedef and typename combined together. And what does " :: " mean in a statement?

+31
c ++ typedef typename
Aug 22 '13 at 15:56
source share
2 answers

typedef defines a new type to use in your code, such as shorthand.

 typedef typename _MyBase::value_type value_type; value_type v; //use v 

typename here lets the compiler know that value_type is a type, not an object inside _MyBase .

:: is the scope of the type. It is like "is in", so value_type "is in" _MyBase . or may also be considered as containing.

+34
Aug 22 '13 at 16:01
source share

typename says that _Mybase::value_type is a type name, so typedef can repeat this fact.

+1
Aug 22 '13 at 16:01
source share



All Articles