Appendix B.  The typename and template Keywords
Team LiB
Previous Section Next Section

Appendix B. The typename and template Keywords

The template keyword is used to introduce template declarations and definitions:



   template <class T>


   class vector;



The typename keyword is often used in place of class to declare template type parameters:[1]

[1] We'll discuss the reasons why this book uses class and not typename in a moment.



   template <typename T>


   class vector;



Both keywords also have a second role in the language. This appendix is about that role, why it is needed, and exactly how to apply typename and template to fill it. Because the rules are subtle, many people wait until the compiler complains before thinking about the use of typename or template, but it's worth learning these technical details because:

  • You'll spend less time fixing trivial syntax errors.

  • You'll understand what you did wrong when the compiler does complain.

  • Your code will be more portablemany compilers don't complain enough to be strictly standards-conforming, and won't tell you when you missed a typename or template.

  • Your code will be more likely to work as you intendthe compiler can't detect all misuses, and leaving one of these keywords out can cause your program to misbehave silently.

    Team LiB
    Previous Section Next Section