Defining Concepts with requires
Define custom concepts using requires expressions and constraints.
Defining Concepts with requires is a free C++ Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.
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.
Learn C++ with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “Defining Concepts with requires” lesson free?
Yes — any 3 lessons of the C++ Academy learning path, including “Defining Concepts with requires”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.
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