Learning More Ways to Specify Constraints

Explore other ways to define constraints in C++20 in this lesson.

Requires clause vs. requires expression

In this section, we’ve discussed requires clauses and requires expressions. Although both are introduced with the new requires keyword, they are different things and should be fully understood:

  • A requires clause determines whether a function participates in overload resolution. This happens based on the value of a compile-time boolean expression.

  • A requires expression determines whether a set of one or more expressions is well formed, without having any side effects on the behavior of the program. A requires expression is a boolean expression that can be used with a requires clause.

Let’s see an example again:

Get hands-on with 1400+ tech skills courses.