1. Problem It Solves
A custom concept gives a meaningful name to a reusable compile-time contract for template arguments. It makes an important assumption visible and checkable.
2. Prerequisites
Requires expressions and templates from Day 7.
You should be able to compile a short program and read its output.
3. Core Idea
A concept is a named gate: a type passes when the Boolean constraint is true, and code behind the gate may rely on that stated contract. Read concept as a precise promise; runtime preconditions still belong to the programmer.
4. Minimal Syntax
template<class T>
concept Addable = requires(T a, T b) { { a + b } -> std::same_as<T>; };5. How It Works
The program introduces the smallest relevant form of
concept.It applies the feature to fixed data while required owners remain in scope.
It prints one result that can be checked against the source.
6. Common Mistakes
A concept that checks incidental syntax instead of real semantic needs can accept misleading types or make an API needlessly restrictive.
Also check the required header, C++20 library support, lifetime, and deduced types.
7. When to Use It
Use it when the same domain rule constrains several templates and deserves a readable name.
Avoid it when the condition is used once and a short standard concept already says exactly the same thing.
8. Simple Example
The Addable concept accepts values whose addition returns the same type and constrains a small twice function. The companion .cpp uses no input, so its result is easy to reproduce.
Complete sample code
Source file
cpp20/08_custom_concepts/main.cpp
// Day 8: Defining Custom Concepts
#include <concepts>
#include <iostream>
template<class T>
concept Addable = requires(T a, T b) {
{ a + b } -> std::same_as<T>;
};
template<Addable T>
T twice(T value) {
return value + value;
}
int main() {
std::cout << "twice(6) = " << twice(6) << '\n';
}
9. Key Takeaways
conceptexpresses the central C++20 idea of this day.The example isolates one behavior with fixed data.
Compiler checks do not replace lifetime and runtime reasoning.
Prefer the smallest interface that states the real requirement.
10. Self-Check Questions
Easy — What is the main job of
conceptin the minimal example?Medium — Why does the concept reject a type whose
operator+returns an unrelated proxy type?Hard — How can a syntactically valid
a + bstill fail to satisfy the intended meaning of “addable,” and what does that teach about concept design?