Let’s think about operations for a little longer. What does it mean to require the support of a + operation?

Constrain the accepted types

It means that we constrain the accepted types to those that have a function T T::operator+(const T& other) const. It can be T T::operator+(const U& other) const as well because maybe we want to add to an instance of another type later on, but that’s not the point here. The important point is that we made a requirement on having a specific function.

We should be able to define a requirement on any function call. Let’s see how to do it.

Note: Try uncommenting line 36 and run the code again to see an unsatisfied constraints error.

Get hands-on with 1400+ tech skills courses.