Concepts (C++20)
Prerequisites: constexpr & compile-time computation
A concept constrains what types a template accepts, checked at the point it's used rather than deep inside the template's body — replacing cryptic, page-long template error messages with a clear "this type doesn't satisfy X" at the actual call site.
#include <concepts>
template<typename T>
concept Numeric = std::integral<T> || std::floating_point<T>;
template<Numeric T>
T add(T a, T b) {
return a + b;
}
add(2, 3); // fine — int satisfies Numeric
add("a", "b"); // fails to compile, clearly: "a"/"b" aren't Numeric
Before concepts, this kind of constraint required std::enable_if
and produced far less readable compiler errors — concepts are now
the preferred way to write constrained templates in C++20 and
later.