C++26: Ordering of constraints involving fold expressions
C++26 introduces a solution to the ambiguity issues in overload resolution involving fold expressions. The new concept of fold expanded constraints allows the compiler to properly recognize and prioritize more constrained overloads. This change addresses limitations present in C++20 and C++23 regarding constraint subsumption.
- ▪C++26 resolves ambiguity in overloads by introducing fold expanded constraints.
- ▪The compiler can now recognize when one constraint subsumes another, even with fold expressions involved.
- ▪This update improves the handling of variadic overloads constrained by fold expressions.
Opening excerpt (first ~120 words) tap to expand
You have two overloads of g(). One requires A<T> for each element in a pack, the other requires C<T> — where C is a stricter concept that subsumes A. Both apply to the types you’re passing. The compiler should pick the more constrained version. But instead it complains about an ambiguous call.This is a limitation of how C++20 and 23 handle constraints that use fold expressions — fixed in C++26.Constraint subsumption, brieflySubsumption is the compiler’s mechanism for ordering overloads by how constrained they are. If concept C is defined as A && B, then any type satisfying C also satisfies A. The compiler recognizes this and, when resolving an overload, prefers the more constrained one — no ambiguity.As we saw a few years back, subsumption has quirks.
…
Excerpt limited to ~120 words for fair-use compliance. The full article is at Sandor Dargo’s Blog.