From the course: C++ Templates and the STL

Unlock this course with a free trial

Join today to access over 22,500 courses taught by industry experts.

Template syntax

Template syntax

- [Instructor] The basic syntax for defining a template is straightforward. Here I have a working copy of template-function.cpp from chapter one of the exercise files. In this case I've defined a template function. Let's take a look at the syntax. The keyword template introduces the template, and type names are listed in the angle brackets, separated by commas, so I can have more than one here. Though keyword type name or class is interchangeable here, I tend to use type name, others tend to use class, just be consistent. In this case the type name is named T. The type name T is an alias that will be replaced by the type used in the template call down here on line 16 we call the template with the name of the function, max of, and we put a type inside angle brackets. This is called the template parameters. This tells the compiler, remember this is a compiler abstraction, it happens at compile time, so this tells the compiler what sort of specialization to use. Here a specialization is…

Contents