Skip to content

Constraints and Concepts (C++20)

One-Liner

A mechanism for specifying semantic requirements on template parameters (such as "hashable" or "iterator"), catching incorrect types early at compile time and producing readable error messages.

#include <concepts>

Core API Cheat Sheet

OperationSignatureDescription
Concept definitiontemplate<...> concept Name = constraint-expression;Defines a named set of constraints
requires expressionrequires { /* 表达式 */ }Checks whether an expression is valid
Nested requirement{ expr } -> std::convertible_to<T>;Requires an expression to be valid and its result convertible to T
Abbreviated function templatevoid f(Concept auto param)Uses concept constraints directly in the parameter list
requires clausetemplate<typename T> requires Concept<T> void f(T);Appends constraints after a template declaration
Trailing requirestemplate<typename T> void f(T) requires Concept<T>;Appends constraints after a function parameter list
Logical ANDConcept1 && Concept2Combines multiple constraints (conjunction)
Logical ORConcept1 || Concept2Combines multiple constraints (disjunction)

Minimal Example

cpp
#include <concepts>
#include <iostream>

template<typename T>
concept Addable = requires(T a, T b) { a + b; };

template<Addable T>
T add(T a, T b) { return a + b; }

int main() {
    std::cout << add(1, 2) << '\n';     // OK: int 满足 Addable
    // add("a", "b");                   // Error: const char* 不满足 Addable
}

Embedded Applicability: High

  • A purely compile-time feature with zero runtime overhead, suitable for resource-constrained environments
  • Constraint-driven design catches type errors at compile time, preventing undefined behavior (UB) on the target board
  • Standard library concepts (such as std::integral, std::same_as) can directly constrain the interfaces of hardware register wrapper types
  • Error messages are significantly shortened, greatly accelerating the development and debugging cycle of low-level template libraries

Compiler Support

GCCClangMSVC
10.010.019.28

See Also


Some content referenced from cppreference.com, licensed under CC-BY-SA 4.0

Built with VitePress