Constraints in C++ 20

What is a constraint?

A constraint is a set of logical operations and operands that specify criteria for template parameters.

There are two main types of constraints:

  1. conjunctions
  2. disjunctions
template<Incrementable T>
void f(T) requires Decrementable<T>;
template<Incrementable T>
void f(T) requires Decrementable<T>; // OK, redeclaration
template<typename T>
requires Incrementable<T> && Decrementable<T>
void f(T); // Ill-formed, no diagnostic required
// the following two declarations have different constraints:
// the first declaration has Incrementable<T> && Decrementable<T>
// the second declaration has Decrementable<T> && Incrementable<T>
// Even though they are logically equivalent.
template<Incrementable T>
void g(T) requires Decrementable<T>;
template<Decrementable T>
void g(T) requires Incrementable<T>; // ill-formed, no diagnostic required

Conjunctions

This is one of the basic types of constraints. Conjunction itself means connections of two or more clauses or sentences.

The conjunction of two or more constraints can be formalized using && operator. We can simply put && operator in between the two constraints in order to form a conjunction.

template <class T>
concept assignable_from = std::is_assignable<T>::value;
template <class T>
concept SignedIntegral = Integral<T> && std::is_signed<T>::value;
template <class T>
concept UnsignedIntegral = Integral<T> && !SignedIntegral<T>;

As we know, how && operator works. It will return true, if both constraints are satisfied, otherwise, we will get a false. So, the conjunction of two or more constraints will be satisfied if and only if every constraint gets satisfied. This expression is evaluated from left to right. It means, if the first constraint returns a false, there is no need to check other constraints.

Disjunctions

This is one of the important constraints. Disjunction itself means the relation of two or more distinct clauses.

The disjunction of two or more constraints can be formalized using || operator. We can simply put || operator in between two constraints in order to form a disjunction.

template <class T = void>
requires EqualityComparable<T> || Same<T, void>
struct equal_to;

As we know, how || operator works. It will return true, if either constraint is satisfied, otherwise, we will get a false. So, the disjunction of two or more constraints will be satisfied if and only if either constraint gets satisfied. This expression is evaluated from left to right. It means, if the first constraint returns true, there is no need to check other constraints.

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved