Search⌘ K
AI Features

The Constrained Template Parameter

Explore how to write constrained template parameters in C++20 using Concepts for more concise and clear template code. Understand the limitations of this approach, especially with complex expressions, and discover how to use concepts that take multiple parameters such as std::same_as.

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 ...