Concepts (C++20)
Concepts name requirements on template arguments: template <std::integral T> constrains T to integer-like types, replacing pages of enable_if gymnastics with a readable bound. Violations fail with a clear message naming the unsatisfied concept instead of a novel-length substitution backtrace.
The standard library ships ready-made concepts (integral, floating_point, same_as, convertible_to, ranges...), and requires-clauses express compound conditions (requires std::integral<T> && sizeof(T) > 1). Concepts don't change what compiles — they change how loudly and clearly misuse fails.
Constrain with a name
template <std::integral T> T twice(T x) accepts ints and longs but rejects strings and doubles at the call site.
Clear errors
A violated concept reports which requirement failed — versus pages of substitution-failure context without it.
Compose with requires
requires std::integral<T> && (sizeof(T) >= 4) stacks conditions; custom concept definitions name reusable bundles.
Constrain, don't SFINAE
New code should reach for concepts first — enable_if remains only for pre-C++20 compatibility.
#include <iostream>#include <concepts> template <std::integral T>T double_it(T x) { return x * 2; } int main() { std::cout << double_it(21) << std::endl; return 0;}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C++ yourself — building beats watching.