C++20 Concepts: Part 5 (Advanced use cases)

Gajendra Gulgulia
8 min readDec 15, 2021

In this article, I’ll explain and demonstrate how to define concepts that constrain multiple template parameters and more importantly how to use them in a generic function. As promised in the part 3 of the series, I’ll explain this using a function comparing for equality of two different types T and U

1. Introduction

So far we’ve only looked at simple use cases of concepts and concept definitions whereby a single template type, T is constrained:

template <typename T>
concept ConceptName = someExpression

Most of the time they may be sufficient but there may be use cases where constraints on two different types may be needed. For e.g. boolean operator on two different types can be a valid operation with the introduction of the three way operator and hence it makes sense to provide constrains for such types or two different types accepted into a generic function should have overloaded std::ostream method or that they should be compatible for arithmetic operations etc.

Concepts with multiple template parameters: bool comparison of different types

Consider the example of the concept BoolComparablein third article :

template<typename T>
concept BoolComparable =…

--

--