C++ Tutorial

Concepts (C++20)

Defining formal constraints on template parameters

What are C++ Concepts?

Introduced in C++20, Concepts are formal requirements (constraints) that a template type must meet to be valid for use. They are the biggest improvement in C++ templates since the language began because they provide clear, readable error messages and replace the complex, "hacky" SFINAE logic of the past.
cpp
#include <concepts>

// Define a Concept: Must be an integer!
template <typename T>
concept Integral = std::is_integral_v<T>;

// Constrain a function!
template <Integral T>
T add(T a, T b) {
    return a + b;
}

// add(1.23, 4.56); // ERROR: double is not Integral!

Why Use Concepts?

Wait! If you passed a string to a math template in the old days, you'd get a 100-line error message from the compiler! With Concepts, the compiler tells you exactly which requirement failed in a single, human-readable sentence.
cpp
// Much clearer error message!
error: constraints not satisfied
note: because 'string' is not an 'Integral'

Creating Complex Concepts: requires

You can combine multiple checks or require that a type has certain methods.
cpp
template <typename T>
concept Printable = requires(T v) {
    // T must have a .toString() method that returns a string!
    { v.toString() } -> std::same_as<std::string>;
};

template <Printable T>
void show(T v) {
    std::cout << v.toString() << std::endl;
}

Logical Helpers: && and ||

Concepts can be combined using standard logical operators and built-in traits from the <concepts> library.
cpp
template <typename T>
concept SignedIntegral = std::integral<T> && std::signed_integral<T>;

Summary

FeatureDescriptionBest Use Case...
conceptA set of requirements for a typeFormal interface for templates
requiresBlock to check methods and propertiesComplex type validation
ConstraintsLimit which types can use your libraryRobust generic libraries
ModernC++20 required!
ReadableBest error messages in C++ history
PerformanceZero runtime cost (all checks at compile-time!)
Standardstd::integral, std::floating_point, std::sortable
CheckUse concept before template for safety!
LogicCombine with && and `
ReplaceReplaces complex enable_if and SFINAE!
AlgorithmModern STL algorithms are now concepts-based!
DeduceInfluences overload resolution perfectly!
MetaThe ultimate tool for modern template meta-programming
Trailingtemplate <typename T> requires Integral<T>
Abbreviatedvoid f(Integral auto x) {} (Extremely clean syntax!)
Returnrequires { { f(x) } -> std::same_as<int>; }
NestedConcepts can depend on other concepts!
TraitWrappers around old type-traits like is_pointer
ConceptFormally document your requirements for others!
CompileErrors are caught immediately, saving developer time
GenericThe final piece of the template puzzle for C++!