A function template in C++ is a blueprint for a family of functions. You write the logic once with a placeholder type, and the compiler generates a concrete function for each type you use, checked at compile time. It removes duplicated overloads and keeps full type safety and speed, because there is no runtime dispatch.
Since C++20 you can also constrain templates with concepts, and the abbreviated syntax auto in parameters creates a function template without writing template.
A function template starts with template <...> followed by an ordinary function declaration that uses the template parameters.
typename T and class T mean the same here. When you call add(5, 10), the compiler deduces T as int from the arguments and instantiates add<int>. Each distinct type creates a separate instantiation in the generated code.
The compiler deduces template parameters from the call arguments. Deduction fails if the same parameter gets conflicting types.
You can fix this by specifying the type explicitly, casting an argument, or using two template parameters:
Return type deduction with auto (C++14) avoids writing a trailing return type. Use std::common_type_t<T, U> when you want the result type to be explicit.
Templates can take values as parameters, such as sizes, which are useful for fixed-size arrays.
The array size N is deduced at compile time from the array type, so no separate length argument is needed. For general ranges, prefer std::span or standard algorithms such as std::accumulate.
A function template can be specialized for a particular type, but overloading is usually the better tool for functions.
Full specialization (template <> ...) works, but it does not take part in overload resolution, which can give surprising results. Function templates do not support partial specialization. Use overloads or a class template helper when you need that. A non-template function wins over a template when both match equally well.
An unconstrained template accepts any type and fails deep inside the body with a long error. Concepts state the requirements up front.
The abbreviated form is shorter for simple cases:
Concepts give readable diagnostics and replace most SFINAE tricks used before C++20.
Because the compiler needs the full definition to instantiate a template, put template definitions in headers (or in the same translation unit as their use). A definition placed only in a .cpp file causes linker errors for types it was not instantiated with there. Explicit instantiation (template int add<int>(int, int);) is an option when you want to keep code in a source file and support a known set of types.
Templates also affect build size. Many instantiations increase binary size and compile time, which is called code bloat. Keep templates small and move type-independent work to ordinary functions.
| Approach | Type checked | One definition | Notes |
|---|---|---|---|
| Overloads | Yes | No, one per type | Simple, but repetitive |
| Function template | Yes | Yes | Instantiated per type, header-based |
| Preprocessor macro | No | Yes | Avoid in C++ |
For template type parameters they are interchangeable. Many style guides use typename because the parameter can be any type, not only a class.
No. Each instantiation is a normal function resolved at compile time, so calls can be inlined. The cost shows up in compile time and binary size.
The definition is not visible where the instantiation is needed, often because it lives in a .cpp file. Move the definition into the header.
Yes. Template parameters can have defaults, for example template <typename T = int>, and function default arguments work as usual.
A function template writes one generic function that the compiler instantiates for each type. Use deduction for convenience, explicit arguments to resolve conflicts, and overloads instead of specialization for functions. Constrain templates with C++20 concepts for clear errors, and keep definitions in headers.