What are C++20 concepts?
Short answer
Concepts are named, compile-time predicates that constrain template parameters—specifying the requirements a type must satisfy (e.g., must be sortable, must support +).
Advertisement
AModel answer
Concepts are named, compile-time predicates that constrain template parameters—specifying the requirements a type must satisfy (e.g., must be sortable, must support +). They make templates clearer, produce far better error messages than raw templates or SFINAE, and let the compiler reject invalid types early with a precise reason.
template <std::integral T>
T doubleIt(T x) { return x + x; } // T must be an integer typeReal-world example
Constraining a numeric template with std::integral gives a clear error when a string is passed.
Likely follow-ups
- What do concepts constrain? | How do they improve error messages?
Related topics