When the template parameter is declared, the prefix keywords class and typename are interchangeable. That is to say, there is no difference between the following two:
(1) template <class T> class widget;
(2) template <typename> class widget.
However, C ++ does not always regard class and typename as equivalent. Sometimes typename must be used.
This is the time: When you want to refer to a nested subordinate type name in the template, you must put the keyword typename in the previous position next to it, except for the following: it cannot be used as a base class modifier in base class list (base class column) or member initialization list (member Initial Value column. Where: If the names in the template depend on a template parameter, they are called subordinate names. If the subordinate name is nested in the class, it is called the nested subordinate name. C: const_iterator (see the example below) is such a name. In fact, it is also a nested subordinate type name, that is, a nested subordinate name and indicates a type. Take the following example:
Template <typename C>
Void print2nd (const C & container)
{
If (container. Size ()> = 2)
{
C: const_iterator ITER (container. Begin ());
++ ITER;
Int value = * ITER;
Cout <value <Endl;
}
}
Nested subordinate names may cause parsing difficulties. The following example (change the above ):
Template <typename C>
Void print2nd (const C & container)
{
C: const_iterator * X;
...
}
According to the normal understanding, we should declare a local variable of X, but the premise is that we know that C: const_iterator is of a type, but if not, for example, if C is a class and it has a static member variable of const_iterator and X is a global variable, then the above is a multiplication action, that is, C: const_iterator multiplied by X. Cause ambiguity.
Before we know what C is, there is no way to know whether C: const_iterator is a type. When the compiler starts parsing the template print2nd, it is still unknown what C is. C ++ has a rule to resolve this ambiguous state: If the parser encounters a nested slave name in the template, it assumes that the name is not of a type unless you tell it to be. Therefore, the nested slave name is not a type by default. This rule has an exception and will be discussed later. We can tell it that you only need to add the keyword typename before C: const_iterator.
Note that typename is only used to verify the nested subordinate type name; other names should not exist. Example:
Template <typename C>
Void F (const C & container, typename C: iterator ITER ). The first parameter is not a nested subordinate type name, so the keyword typename is not required, but the second parameter is required.
The exception to the rule "typename must be the prefix word of the nested subordinate type name" is that typename cannot appear before the nested subordinate type name in the base classes list, it cannot be used as a base class modifier in the member initalization list (initial member column. For example:
Template <typename T>
Class derived: public base <t >:: nested // "typename" is not allowed in the base list"
{
Public:
Explicit derived (int x)
: Base <t>: Nested (x) // "typename" is not allowed in MEM init list"
{
Typename base <t >:: nested temp; // This must have "typename"
}
}
One final point of Declaration: the above mentioned does not apply in VC ++ 6.0, that is to say, in VC, the nested subordinate type is not modified using typename, And the compiler will not complain, however, complaints may occur in the GCC kernel.
References: Objective C ++