Concepts
A concept is a named, compile-time predicate on types, added in C++20. Attaching one to a template says what the template requires, so the compiler rejects bad arguments at the call site with a readable message.
Detailed Explanation
A template body imposes requirements without ever stating them. template <typename T> T maximum(T a, T b) { return a < b ? b : a; } requires that T supports operator< and is copyable, but nothing in the signature says so. Pass a type without operator< and the compiler reports the failure from inside the body, often after descending through three layers of standard library headers. A concept moves that requirement into the signature.
Before C++20, the workaround was SFINAE, short for "substitution failure is not an error". You would bury a check in the return type or an extra defaulted template parameter using std::enable_if_t. It worked, but it was write-only code: the constraint was smeared across the declaration, a small mistake produced silent removal from the overload set rather than an error, and diagnostics were as bad as before. Concepts replace all of that with a name you can read and reuse.
The syntax has several spellings for the same idea. A concept is defined with template <typename T> concept Name = <boolean constant expression>;. You then apply it in whichever form reads best:
- As the parameter's kind:
template <Sortable T> void sortAll(T& c); - With a
requiresclause:template <typename T> requires Sortable<T> void sortAll(T& c); - As an abbreviated function template:
void sortAll(Sortable auto& c);— notemplateline at all - Unconstrained abbreviated form:
void print(auto value);is shorthand for a one-parameter template
The building block is a requires expression, which is not the same thing as a requires clause. A requires expression is a compile-time question: "would this code be valid?" Inside it you list expressions that must compile. You can also state the required result type with a compound requirement such as { a + b } -> std::same_as<T>;. It never evaluates anything at runtime; it only checks well-formedness.
The standard library ships a set of ready-made concepts in <concepts>: std::integral, std::floating_point, std::same_as, std::convertible_to, std::equality_comparable, std::totally_ordered, std::invocable, std::copyable, std::movable. The iterator and range concepts live in <iterator> and <ranges> and are what makes C++20 ranges compose.
Concepts do more than reject bad types. They participate in overload resolution by subsumption. If one overload requires std::integral and another requires std::integral && sizeof(T) == 8, the compiler recognises the second constraint as strictly stronger and picks it, with no ambiguity and no tag types. This is the clean replacement for tag dispatch and for the chain of enable_if overloads people used to write. Subsumption compares the atomic constraints structurally, which is why naming a concept once and reusing the name matters: two textually identical but separately written expressions are not treated as the same constraint.
The cost is small. Constraint checking happens at compile time and generally speeds up compilation, because failures are detected before the body is instantiated. The real cost is that concepts are C++20, so you need a recent toolchain, and a codebase pinned to C++17 has to keep using enable_if or static_assert.
Code Examples
#include <concepts>
#include <iostream>
#include <string>
// A concept built from a requires expression.
template <typename T>
concept Addable = requires(T a, T b) {
{ a + b } -> std::convertible_to<T>; // a + b must compile and yield T
};
// Three equivalent ways to constrain the same function.
template <Addable T> T twice(T v) { return v + v; }
template <typename T> requires Addable<T> T thrice(T v) { return v + v + v; }
auto quadruple(Addable auto v) { return v + v + v + v; }
int main() {
std::cout << twice(21) << '\n'; // prints: 42
std::cout << thrice(std::string("ab")) << '\n'; // prints: ababab
// twice(&main); // error at the call site: does not satisfy Addable
}#include <concepts>
#include <iostream>
// Subsumption: the more constrained overload wins, no ambiguity.
template <std::integral T>
void describe(T) { std::cout << "integer\n"; }
template <std::integral T>
requires (sizeof(T) == 8)
void describe(T) { std::cout << "wide integer\n"; }
int main() {
describe(short{1}); // prints: integer
describe(1LL); // prints: wide integer
describe(3.5); // error: no overload accepts a double
}Common Gotchas
A `requires` expression only checks that code is well-formed
`requires(T a) { a.close(); }` is satisfied by any type with a callable `close()`, whatever it does. Concepts constrain syntax, not semantics; the semantic promises stay in your documentation.
`requires requires` is not a typo, but it is a smell
`template <typename T> requires requires(T a) { a.size(); }` puts an unnamed constraint inline. It compiles, yet it defeats subsumption and reuse. Give the constraint a name.
Identical-looking constraints do not subsume each other
two overloads that each spell out `sizeof(T) == 8` inline are distinct atomic constraints and produce an ambiguity. Define the predicate once as a named concept and use that name everywhere.
`auto` parameters make an implicit template
`void log(auto value)` is a function template, so its definition must be visible where it is called, exactly like any other template.