Syntax

C++ Function Template Syntax

In C++, function template: writes a function for any type — template <typename T> T maxOf(T a, T b) { return a > b ? a : b; }

Quick Answer

Syntax
template <typename T> T maxOf(T a, T b) { return a > b ? a : b; }
C++Syntax

Function Template

Code example
template <typename T> T maxOf(T a, T b) { return a > b ? a : b; }

Writes a function for any type.

Learn More

How to function template in C++

  1. Open your C++ file or editor.
  2. Copy the example, then replace its sample names and values for your code.

C++ Function Template Syntax

Use template <typename T> T maxOf(T a, T b) { return a > b ? a : b; }.

Related Syntax References

Frequently asked questions

What is the C++ syntax for function template?

Use this pattern: template <typename T> T maxOf(T a, T b) { return a > b ? a : b; }

Can I copy this C++ code example?

Yes. Use Copy code and adapt the example to your program.

Is this C++ syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify