The Constrained Template Parameter

Learn how to use the constrained template parameter to write concepts.

How to write constrained template parameters

Constrained template parameters are a bit terser than the ones we’ve discussed so far. They also have some limitations.

template <Number T>
auto add(T a, T b) {
  return a+b;
}

As we can see, we don’t need any requires clause here. We can simply define a requirement on our template parameters right where we declare them. We use a concept name instead of the keyword typename. We’ll achieve the very same result as with the previous two methods.

Let’s check it out in the example below.

Get hands-on with 1400+ tech skills courses.