TITLE: typedef essential for template programming (Source: comp.lang.c++.moderated, 9 Jan 2001) ALEXANDRESCU: "Andrei Alexandrescu" >> Typedef is much more essential in template programming than in the >> example above. Typedef essentially introduces a new type into a >> context, much as you introduce a new value by defining one. RICH: "Rich aka "Phil"" > You can't create new types with typedef, only aliases for existing > types. ALEXANDRESCU: True. Please note that I said "introduce" not "create" :o). What I was trying to say was that typedef introduces a new _symbol_ for a type in a context. In fact, I came to the conclusion that typedef does for types pretty much what const does for values. Let's take another example. Consider lists of types built on the following basic structure: template struct Typelist {}; You can build typelists of any length by composing Typelist with itself: typedef Typelist > > CharTypes; (NullType is an insipid type put by convention at the end of a typelist.) Now say you want to find a type at a given position in a typelist. You can do that with the following way: template struct TypeAt; template struct TypeAt, 0> { typedef Head Result; }; template struct TypeAt, i> { typedef typename TypeAt::Result Result; }; By now I think it's clear just how germane typedef is to a compile-time computation like the above. A type introduced through typedef stores both the intermediary results and the final result of a computation. It is true that with typedef you can only create aliases for existing types, but that's one of those "true but uninteresting" statements that don't take one too far. After all, all programs are sequences of characters :o). _______________________________________________ cpptips mailing list http://cpptips.hyperformix.com