Defining Concepts with requires
Define custom concepts using requires expressions and constraints.
Defining Concepts with requires is a free C++ Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The concept Keyword
Declare a concept with template <params> concept Name = expression; The expression must be a compile-time boolean.
template <typename T>
concept Integer = std::is_integral_v<T>;Combining Type Traits
Use logical operators to combine traits into a concept.
template <typename T>
concept SignedInteger = std::is_integral_v<T> && std::is_signed_v<T>;The requires Expression
A more expressive form: list the operations the type must support.
template <typename T>
concept Addable = requires(T a, T b) {
a + b;
};Compound Requirements
Inside requires, you can specify the result type and additional constraints.
template <typename T>
concept Number = requires(T a, T b) {
{ a + b } -> std::convertible_to<T>;
{ a * b } -> std::convertible_to<T>;
};Nested Requirements
Add requires inside a requires expression to bring in named concepts.
template <typename T>
concept Sortable = requires(T t) {
{ t.begin() };
{ t.end() };
requires std::movable<typename T::value_type>;
};Type Requirements
You can require that a type has certain nested types or aliases.
template <typename T>
concept HasValueType = requires {
typename T::value_type;
};Using Concepts in Templates
Three syntaxes:
// 1. Replace typename
template <Integer T>
T increment(T x) { return x + 1; }
// 2. requires after template parameters
template <typename T>
requires Integer<T>
T increment(T x) { return x + 1; }
// 3. requires after function signature
template <typename T>
T increment(T x) requires Integer<T> {
return x + 1;
}Concepts and auto Parameters
Abbreviated function templates can also be constrained.
void process(Integer auto x) { std::cout << x; }Multiple Constraints
Combine concepts with && and ||.
template <std::integral T, std::integral U>
requires (sizeof(T) >= sizeof(U))
T promote(U value) { return static_cast<T>(value); }Naming Conventions
Most code uses CamelCase for concepts. Some libraries use lowercase (matching the standard library). Pick a convention and stick to it.
Concepts in Class Templates
Constraint class templates the same way.
template <std::integral T>
class Counter { T value_; /* ... */ };Reusable Concepts
Define a library of small concepts and compose them. Reusable concepts make templates self-documenting and easy to maintain.
Quick Check
Which syntax inside a requires expression checks that the result of a + b is convertible to T?
Recap
Define concepts with concept Name = expression. Use requires expressions for operation-based constraints with compound, nested, and type requirements. Combine concepts with && and || to express rich constraints.
Frequently asked questions
Is the “Defining Concepts with requires” lesson free?
Yes — the full text of “Defining Concepts with requires” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.
What will I learn in “Defining Concepts with requires”?
Define custom concepts using requires expressions and constraints. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C++ Academy?
No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Defining Concepts with requires” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C++ Academy lesson?
Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Why Concepts vs SFINAE
- Defining Concepts with requires
- Using Concepts in Templates and auto
- Common Standard Concepts Integral Ranges