← Back to C++ Course | Chapter 11: Templates | Lesson 2 of 5

C++ Function Templates

A function template is one recipe that works for many types, like a machine that can compare numbers, letters or words. You write it once with a blank for the type.
Syntax
cpp
template <typename T>
T function_name(T param1, T param2) {
  return expression;
}

function_name<type>(arg1, arg2);  // explicit type
function_name(arg1, arg2);        // type deduced

Defining Function Templates

A function template defines a generic function blueprint: you write the template keyword followed by a list of type parameters in angle brackets, then a normal-looking function signature that uses those parameter names in place of concrete types.

Example: Defining Function Templates

cpp
// Include std::cout and std::cin
#include <iostream>

// Declare a template so this works with any suitable type
template <typename T>
T square(T x) {
	// Send x * x back to the caller
	return x * x;
}

// Program execution starts in main()
int main() {
	// Print to the console with cout
	std::cout << square(5) << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Calling Function Templates

You can call a function template explicitly by specifying the type inside angle brackets yourself, or simply call it like a normal function and let the compiler deduce the type automatically from the arguments you pass — the latter is far more common in practice.

Example: Calling Function Templates

cpp
// Include std::cout and std::cin
#include <iostream>

// Declare a template so this works with any suitable type
template <typename T>
T square(T x) {
	// Send x * x back to the caller
	return x * x;
}

// Program execution starts in main()
int main() {
	// Print to the console with cout
	std::cout << square<double>(2.5) << std::endl;
	// Print to the console with cout
	std::cout << square(4) << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Multiple Template Parameters

A single function template can declare multiple type parameters, letting one generic function handle several arguments of genuinely different, unrelated types in the same call rather than requiring them all to match.

Example: Multiple Template Parameters

cpp
// Include std::cout and std::cin
#include <iostream>

// Declare a template so this works with any suitable type
template <typename T1, typename T2>
void showPair(T1 a, T2 b) {
	// Print to the console with cout
	std::cout << a << " " << b << std::endl;
}

// Program execution starts in main()
int main() {
	showPair(5, "hello");
	// Return 0 to signal that the program finished successfully
	return 0;
}

Overloading Function Templates

You can overload a function template just like an ordinary function, either by providing a specific non-template overload for a particular type or by defining another template with a different number of parameters, and the compiler picks the best match.

Example: Overloading Function Templates

cpp
// Include std::cout and std::cin
#include <iostream>

// Declare a template so this works with any suitable type
template <typename T>
T maxValue(T a, T b) {
	// Send (a > b) ? a : b back to the caller
	return (a > b) ? a : b;
}

int maxValue(int a, int b, int c) {
	// Send maxValue(maxValue(a, b), c) back to the caller
	return maxValue(maxValue(a, b), c);
}

// Program execution starts in main()
int main() {
	// Print to the console with cout
	std::cout << maxValue(3, 7) << std::endl;
	// Print to the console with cout
	std::cout << maxValue(3, 7, 5) << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Non-Type Template Parameters

Templates also accept non-type parameters — constant values like integers rather than types — which the compiler treats as compile-time constants inside the function body, useful for things like fixed buffer sizes baked in at instantiation.

Example: Non-Type Template Parameters

cpp
// Include std::cout and std::cin
#include <iostream>

// Declare a template so this works with any suitable type
template <int N>
int multiplyByN(int x) {
	// Send x * N back to the caller
	return x * N;
}

// Program execution starts in main()
int main() {
	// Print to the console with cout
	std::cout << multiplyByN<3>(5) << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Passing arguments of different types like max(3, 4.5) and getting a deduction error, since both must give the same T.
  2. Forgetting template <typename T> above the function.
  3. Using an operation that the type does not support, such as < on a type without it.
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.