Back to results for “Concepts & Constraints”
C++ › Concepts & Constraints

How do you define a custom concept?

Advanced

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.

Common follow-ups: How do you compose concepts? | What is a refined concept?