Why do I need to add “template” and “typename” in the bodies of template definitions?
The meaning of a name used in a template definition may depend upon the template parameters, in which case it cannot automatically be determined when the template is defined. Early implementations of templates postponed resolution of all names used in a template to the time of instantiation, but this was found to be error-prone. It made it impossible to parse template definitions completely because many C++ syntax rules depend on distinguishing between names that refer to objects or functions, names that refer to types, and names that refer to templates. Later implementations parse templates as soon as they are defined. They require the programmer to specify which dependent names refer to types or templates, so that they can parse the templates without ambiguity. Where a dependent name is intended to refer to a type, it must generally be prefixed by the keyword typename. (This is not necessary when it is used in the list of base classes of a class template, since only type names can be
Related Questions
- The Center is international, independent and neutral, and is assisted in its operation by advisory bodies composed of external experts in international dispute resolution and intellectual property. Which dispute-resolution procedures does the Center offer?
- Why do I need to add "template" and "typename" in the bodies of template definitions?
- What is the template typename keyword used for?