CKTIS / C++
template — declaring templates and disambiguating names
The template keyword introduces templates: families of related declarations parameterized by types, values, or other templates. It also appears in explicit specialization, explicit instantiation, and syntax that identifies a dependent member name as a template.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
template <typename T> declaration
template <typename T> requires constraint declaration
template <> declaration
template declaration
extern template declaration
object.template member<Type>(arguments)template defines families of classes, functions, and variables, aliases for families of types, and concepts. The header template <typename T> declares a type parameter T; class T means the same thing here. In C++23, parameters can also be values, such as int N, or templates. An optional requires clause constrains acceptable arguments. Template declarations belong at namespace or class scope, not inside a function block.
A template parameter is declared in the parameter list and need not have a name. For example, template<class> struct X; declares one unnamed type parameter. A template argument is the concrete type, value, or template supplied when the template is used. In the example, larger(3, 7) deduces T as int, whereas larger<int>(4, 2) supplies that type explicitly. For the selected type, the expression a < b must be valid. Calling larger(3, 7.0) does not deduce a common type: the two arguments produce conflicting deductions for T.
Distinguish the other uses: template <> introduces an explicit specialization, a separate declaration or definition for particular arguments. Explicit instantiation without empty angle brackets <> after template, such as template int larger<int>(int, int);, requests instantiation of the existing template. The form extern template int larger<int>(int, int); is an explicit instantiation declaration, not a new implementation.
In code dependent on template parameters, object.template member<int>() tells the parser that member is a template and that < starts an argument list rather than a comparison. This disambiguates syntax; it does not declare a new template.
This entry targets C++23. The supplied source is an evolving standard draft, not a frozen C++23 edition; this explanation omits later extensions. The example places its definition before use. Expected output does not imply an executed test or an attested production environment.
Examples
#include <iostream>
template <typename T>
T larger(T a, T b) {
return a < b ? b : a;
}
int main() {
std::cout << larger(3, 7) << '\n';
std::cout << larger<int>(4, 2) << '\n';
}
Result
7 4
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.