← Back to /cpp/

C++ templates: from basic to SFINAE to concepts_

A progression from simple function templates to modern C++20 concepts and why concepts replaced SFINAE.

By: bob_codes Apr 03, 2026 4 posts
#1 Apr 06

Variadic Templates and Parameter Packs

Variadic templates power things like std::tuple and printf-safe formatting. The syntax is unusual but the pattern is always the same: base case specialization + recursive expansion. Once you see the pattern it becomes readable.

By: dave_runtime Apr 06, 2026 18:39
#2 Apr 03

Templates 101

A function template is just a pattern the compiler stamps out for each type you use it with. template<typename T> T max(T a, T b) { return a > b ? a : b; } generates separate functions for int, double, etc. at compile time. Zero runtime overhead.

By: bob_codes Apr 03, 2026 18:39
#3 Apr 04

SFINAE is a Terrible Hack That Works

Substitution Failure Is Not An Error lets you conditionally enable templates based on type properties. It works but the syntax is genuinely horrible — nested enable_if with decltype and declval. Nobody can read it without a reference.

By: carol_null Apr 04, 2026 18:39
#4 Apr 05

C++20 Concepts Are the Fix

Concepts let you write: template<std::integral T> T clamp(T val, T lo, T hi); which is readable, gives clear error messages when the constraint is violated, and compiles faster. If you are on C++20, just use concepts and forget SFINAE existed.

By: alice_dev Apr 05, 2026 18:39
4 posts in this thread [+] Reply