Developer knowledge network · moderated exchange

Супольнасць UnreliableCode

Супольнасць распрацоўшчыкаў даследаванняў, зваротнага праектавання і кадавання

Knowledge indexжыць
4Categories
919Threads
2.8KПаведамленні
Discussion

C++20 Concepts vs SFINAE: How requires clauses simplify template error messages [StackOverflow Architecture Guide]

modern_cpp_artisan
C++ Template Wizard
MEMBER
прадстаўнік: 124
Дата далучэння: Jun 2019
Паведамленні: 29
Дзякуй: 72
1 месяцаў таму · Jul 21, 2026 7:53 PM
#1

Before C++20, constraining a template with SFINAE required verbose std::enable_if_t boilerplate that resulted in 200-line compiler error cascades when constraints failed.

In C++20, concepts allow clean declarative constraints:

CPP
template<typename T>
concept Numeric = std::is_arithmetic_v<T>;

template<Numeric T>
T Add(T a, T b) {
    return a + b;
}

If someone passes std::string, Clang outputs: 'std::string' does not satisfy concept 'Numeric'. One clean, human-readable error line instead of a wall of template substitution failures!

cpp_concurrency_guru
C++ Standards Expert
MEMBER
прадстаўнік: 47
Дата далучэння: Feb 2018
Паведамленні: 17
Дзякуй: 75
1 месяцаў таму · Jul 21, 2026 11:44 PM
#2

Concepts also allow overload resolution based on constraint subsumption (more specific concept overloads beat general ones automatically).

llvm_compiler_dev
LLVM & Clang Hacker
MEMBER
прадстаўнік: 139
Дата далучэння: Jul 2018
Паведамленні: 21
Дзякуй: 15
1 месяцаў таму · Jul 22, 2026 2:46 PM
#3

Migrated our math engine from std::enable_if to concepts. Compilation times dropped by 18% because the compiler doesn't need to eagerly evaluate failed SFINAE template instantiations.