正常
Constraints and Concepts (C++20)
In a Nutshell
A mechanism for specifying semantic requirements for template parameters (such as "hashable" or "iterator"), which intercepts incorrect types at compile time and produces readable error messages.
Header File
cpp
<concepts>Core API Cheat Sheet
| Operation | Signature | Description |
|---|---|---|
| Concept definition | template <...> concept Name = ...; | Defines a named set of constraints |
| requires expression | requires { expression; } | Checks if an expression is valid |
| Nested requirement | requires expression; | Requires expression validity and result convertible to T |
| Abbreviated function template | void func(C auto& x) | Uses concept constraints directly in parameter list |
| requires clause | template<...> requires ... | Appends constraints after template declaration |
| Trailing requires | void func(...) requires ... | Appends constraints after function parameter list |
| Logical AND | C1 && C2 | Combines multiple constraints (conjunction) |
| Logical OR | C1 || C2 | Combines multiple constraints (disjunction) |
Minimal Example
Expand (23 lines)Collapse
cpp
#include <concepts>
#include <vector>
#include <print>
// Define a concept: 'T' must be an integral type
template<typename T>
concept Integral = std::is_integral_v<T>;
// Use concept to constrain function template
// Only accepts types satisfying the Integral concept
auto add(Integral auto a, Integral auto b) {
return a + b;
}
int main() {
// OK: int satisfies Integral
std::println("{}", add(1, 2));
// Compile Error: double does not satisfy Integral
// std::println("{}", add(1.0, 2.0));
return 0;
}Embedded Applicability: High
- Pure compile-time feature with zero runtime overhead, suitable for resource-constrained environments.
- Constraint-driven design intercepts type errors at compile time, avoiding undefined behavior on the target board.
- Standard library concepts (such as
std::integral,std::floating_point) can directly constrain interfaces of hardware register wrapper types. - Significantly shortens error messages, accelerating the development and debugging cycle of low-level template libraries.
Compiler Support
| GCC | Clang | MSVC |
|---|---|---|
| 10.0 | 10.0 | 19.28 |
See Also
Part of the content references cppreference.com, licensed under CC-BY-SA 4.0