TITLE: details on using typename (Newsgroups: comp.std.c++, 6 Feb 98) OTTINGER: Tim Ottinger > Where do I catch up on the new typename keyword, meaning the > intended usage, explanation of need, and maybe an example usage > or two? VANDEVOORDE: David Vandevoorde Allow me to plug a book I am writing for Addison-Wesley ;-) It's a companion book for Bjarne Stroustrup's 3rd edition of "The C++ Programming Language" (which you should definitely get!) that discusses solutions to select exercises proposed by Stroustrup. Don't run to your local bookshop just yet though... publication is still several months away. Here is a short excerpt from my draft. I removed cross-refs and compromised on typesetting. [... excerpt start ...] The keyword typename is used to indicate that the name following the keyword does in fact denote the name of a type. However, you cannot just liberally sprinkle your code with typename wherever you name a type. More precisely, you _must_ use the keyword typename in front of a name that: (1) is qualified: i.e., it contains a scope-resolution operator :: ; and (2) appears in a template; and (3) has a component left of a scope resolution operator that depends on a template _parameter_ (not a template _argument_); and (4) denotes a type; and (5) is not used in a list of base-classes or as an item to be initialized by a constructor initializer list. Furthermore, you are _only allowed_ to use the keyword in this sense if all of the above apply, except perhaps the third point. To illustrate this rather subtle rule, consider this erroneous code fragment: template struct S: typename2 X::Base { S(): typename3 X::Base(typename4 X::Base(0)) {} typename5 X f() { typename6 X::C *p; // Declaration of pointer p X::D *q; // Multiplication! } typename7 X::C *s_; }; struct U { typename8 X::C *pc_; }; Consider each occurrence of typename in this example. They are numbered with subscripts for easy reference. The first, typename1, indicates a template argument: the rules above do not apply to this first usage. The second and third typenames are disallowed by the fifth item in the rules above: base-class names in these two contexts cannot be preceded by typename. However, typename4 is required: here, the name of the base-class is not used to denote what is being initialized or derived from. Instead, the name is part of an expression to construct a temporary X::Base from its argument 0 (a sort of conversion, if you will). The fifth typename is prohibited because the name that follows it, X, is not a so-called qualified name. The sixth occurrence is required if this statement is to declare a pointer. The next line omits the typename keyword and is therefore interpreted by the compiler as a multiplication. This illustrates the fundamental reason why typename was introduced into the language: it allows compilers to better "understand" the intent of your templates, thereby producing earlier and more accurate diagnostics when the code contains syntax errors. The seventh typename is optional because it satisfies all the rules above, except the third. Finally, typename8 is prohibited because it is not used inside a template. [... excerpt end ...] [snip]