1

Possible Duplicate:
Where and why do I have to put the “template” and “typename” keywords?

I was looking through this boost::multi_array example and it has a typedef such as:

template <typename Array>
...
typedef typename Array::template subarray<2>::type::iterator iterator2;

I understand what the

typedef typename <type> <new_type>;

signature looks like, but this has three things after the typedef typename and I can't find online what that would be called or what it does. Can somebody break down what that typedef does and why it has three things? I don't think it's boost-specific.

Community
  • 1
  • 1
tpg2114
  • 14,112
  • 6
  • 42
  • 57

4 Answers4

2

The template in Array::template works exactly like the typename you already understand. Its role in the typedef is to tell (promise) the compiler that subarray is in fact a template and thus the <2> makes sense because of it.

This is exactly the same as the role of the typename keyword in that typedef, but with the effect of telling the compiler it's a template instead of a type.

Flexo
  • 87,323
  • 22
  • 191
  • 272
1

It is the old

typedef typename <type> <new_type>;

you already understand.

type is typename Array::template subarray<2>::type::iterator

new_type is iterator2

jpalecek
  • 47,058
  • 7
  • 102
  • 144
1

Whenever you write a template function/class, the compiler sometimes needs some help. In this example, Array::template is a type inside of templated type, which makes it require the typename.

Without this need, that typedef would just be:

typedef Array::template subarray<2>::type::iterator iterator2;

However, this will (usually generate) a compiler error. To fix this error, simply insert the typename keyword.

typedef typename Array::template subarray<2>::type::iterator iterator2;
syvex
  • 7,518
  • 9
  • 43
  • 47
0

The typename is a helper in this case to let the compiler know this is a type. The keyword is used in templates in instances where the compiler would have trouble choosing between a dependent type and a qualified type.

rerun
  • 25,014
  • 6
  • 48
  • 78