77

In a C++ header file, I am seeing this code:

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.

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.

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?

perror
  • 7,071
  • 16
  • 58
  • 85
Arvind
  • 6,404
  • 20
  • 94
  • 143
  • 1
    `typename` has the same use inside and outside of a `typedef`. A more realistic (sort of) example could be `typedef typename std::vector::iterator Iter;` – chris Aug 22 '13 at 15:57
  • @chris what exactly does _Mybase::value_type and value_type separately, and "_Mybase::value_type value_type" represent? – Arvind Aug 22 '13 at 15:58
  • 8
    And omg, Schildt is back. Burn that book right now. – chris Aug 22 '13 at 15:58
  • @Arvind At the time the template is encountered, you don't know squat about the base type. The statement you're referring to is the language mechanism used to tell the compiler "this thing will actually be a type name when it gets used." (And I can't believe Herby Shildt is *still* writing. Last I checked you could only find his books in the Library of Congress). – WhozCraig Aug 22 '13 at 16:00
  • @Arvind, The :: means it's nested inside `_Mybase`. The `typedef` and the `typename` are two separate matters, one answered quite nicely in that link. – chris Aug 22 '13 at 16:00
  • @chris- you are right, but schildt does have one use- its a factual encyclopaedia of c++- not teachings, but dry facts, as in a boring history lesson. :) – Arvind Aug 22 '13 at 16:00
  • 2
    Read: "what are these three unrelated C++ features that I didn't read about in my book?" – Lightness Races in Orbit Aug 22 '13 at 16:03
  • 6
    @Arvind: Unfortunately, many of those "facts" are _wrong_. – Lightness Races in Orbit Aug 22 '13 at 16:04
  • 4
    "Typename can be substituted by keyword class" lol, false. It's amazing how often a *random quote* from Schildt's writings fails to be correct. – R. Martinho Fernandes Aug 22 '13 at 16:06
  • @Arvind yes, if you think of an history lesson on how the Romans battled Genghis Khan for control of the Americas. – R. Martinho Fernandes Aug 22 '13 at 16:10
  • @R.MartinhoFernandes typename can be substituted by class in the context of a simple template declatation, i.e., template can equally by templae – pippin1289 Aug 22 '13 at 16:10
  • 1
    @pippin1289, But not in the case at hand. – chris Aug 22 '13 at 16:11
  • @pippin1289 I know. It doesn't change the fact that the text misleads to you think it is substitutable in this particular context. Note how it's all in a single sentence and with comma abuse to boot. (I am assuming our asker is quoting properly) – R. Martinho Fernandes Aug 22 '13 at 16:11
  • @chris Yes indeed, I just was providing clarification to the OP so he might get a complete understanding – pippin1289 Aug 22 '13 at 16:12

2 Answers2

84

typedef is defining a new type for use in your code, like a shorthand.

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.

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.

Kadam Parikh
  • 422
  • 4
  • 17
pippin1289
  • 4,861
  • 2
  • 22
  • 37
  • 17
    Is 'typename' necessary here? – johngreen May 10 '16 at 05:47
  • 8
    That depends on the context. If _MyBase is dependent on a template parameter than yes (if _MyBase is a template type), otherwise you shouldn't have to. If you are confused on your context, the compiler will help you by giving an error. – pippin1289 May 10 '16 at 15:02
  • 5
    why `typename ` is necessary when _MyBase is a template type? and why `typename` is not necessary when _MyBase is not a template type? – Guokas Aug 08 '19 at 09:13
  • 4
    @V.Wu Loosely, this is because the compiler is not able to peer inside of the template type until it has been used (instantiated). So in order for this to be valid syntax the programmer must clarify whether value_type is a type, i.e., with typename or not (its a member variable or method). – pippin1289 Aug 09 '19 at 12:50
7

the typename is saying that _Mybase::value_type is the name of type so the typedef can reley on that fact.

Paul Evans
  • 27,315
  • 3
  • 37
  • 54
  • 13
    What changes if I don't include it? Does it not compile, are there some warnings missing? I thought typedef A B; works fine in all cases. – masterxilo May 30 '14 at 13:57
  • 7
    It would be great if someone can answer on @masterxilo's comment because I was just thinking the same thing. – RastaJedi Feb 28 '16 at 08:18
  • 2
    @RastaJedi In Visual Studio at least, `typename` seems to be not required in this case. It is even wrong to put it unless the class you're currently defining is templated: `typedef int Int; template struct B { typedef typename Int IInt; };` is fine but `typedef int Int; struct B { typedef typename Int IInt; };` is not. – masterxilo Feb 28 '16 at 16:06