Advertisement
Advanced C++, Concepts & Constraints

How do you define a custom concept?

Short answer

Define a concept with the concept keyword equal to a compile-time boolean expression, often a requires expression listing required operations and their constraints. The concept can then constrain templates.

Save C++ mock interview
Advertisement
AModel answer
Define a concept with the concept keyword equal to a compile-time boolean expression, often a requires expression listing required operations and their constraints. The concept can then constrain templates. Compose concepts with && and || and refine existing ones to build precise, reusable requirements.
template <class T>
concept Container = requires(T c) {
    c.begin(); c.end(); typename T::value_type;
};
Real-world example
A project defines a Container concept so generic algorithms clearly require begin/end and value_type.
Likely follow-ups
  • How do you compose concepts? | What is a refined concept?
Related topics