Skip to content

Template Basics ​

Templates are C++'s core mechanism for generic programming—write the code once, and it adapts to many types. In this chapter we first look at how templates turn types into compile-time parameters so that one piece of logic serves many types, and along the way we draw a clear division of labor between templates and the virtual functions from the previous chapter; then we start with function templates and see how the compiler deduces type parameters for us automatically; from there we move on to class templates and what sets them apart from ordinary classes; finally we get a first taste of template specialization, so you know what to do when a particular type calls for special handling. This is templates at the entry level—the advanced volumes will go much deeper.

Chapter Contents ​

pdf-latest-4-g85128cc · 85128cc · 2026-10-05