1. Problem It Solves
When several constrained overloads are viable, subsumption helps the compiler choose the one whose constraint is more specific. It makes an important assumption visible and checkable.
2. Prerequisites
Custom concepts, standard concepts, and overloads.
You should be able to compile a short program and read its output.
3. Core Idea
Think of nested gates: every signed integral is integral, so the signed gate is narrower. A value passing both should enter the narrower overload. Read subsumption as a precise promise; runtime preconditions still belong to the programmer.
4. Minimal Syntax
template<class T> concept SignedInt = std::integral<T> && std::signed_integral<T>;5. How It Works
The program introduces the smallest relevant form of
subsumption.It applies the feature to fixed data while required owners remain in scope.
It prints one result that can be checked against the source.
6. Common Mistakes
Repeating logically equivalent Boolean expressions instead of building one concept from another may prevent the compiler from recognizing the intended ordering.
Also check the required header, C++20 library support, lifetime, and deduced types.
7. When to Use It
Use it when overloads form a genuine general-to-special hierarchy.
Avoid it when a single function with straightforward behavior is clearer than a constraint hierarchy.
8. Simple Example
One overload accepts all integral types and another accepts the named, more specific signed-integral concept. The companion .cpp uses no input, so its result is easy to reproduce.
Complete sample code
Source file
cpp20/11_concept_subsumption_overload_resolution/main.cpp
// Day 11: Concept Subsumption and Overload Resolution
#include <concepts>
#include <iostream>
template<class T>
concept Integer = std::integral<T>;
template<class T>
concept SignedInteger = Integer<T> && std::signed_integral<T>;
void classify(Integer auto) {
std::cout << "integer\n";
}
void classify(SignedInteger auto) {
std::cout << "signed integer\n";
}
int main() {
classify(1);
classify(1u);
}
9. Key Takeaways
subsumptionexpresses the central C++20 idea of this day.The example isolates one behavior with fixed data.
Compiler checks do not replace lifetime and runtime reasoning.
Prefer the smallest interface that states the real requirement.
10. Self-Check Questions
Easy — What is the main job of
subsumptionin the minimal example?Medium — Which overload is called for
int, and which one is called forunsigned int?Hard — Why can two constraints that humans see as logically equivalent remain unordered if they were written as separate atomic constraints?