C++ Intermediate · Lesson 19 of 27
SFINAE basics
Substitution failure is not an error: enable_if and choosing overloads by type.
Estimated time · 60–90 min
Learning goals
What you will understand
- Explain sfinae basics in your own words
- Use it in a small robotics example
- Verify the result instead of assuming it worked
Understand the idea first
The mental model
SFINAE stands for 'substitution failure is not an error': if substituting a type into a template's signature fails, the compiler quietly drops that overload instead of reporting an error. std::enable_if_t<condition, T> is the standard way to switch an overload on or off.
This lets you provide one function for integral types and another for floating-point types. In C++17 prefer if constexpr when one function body can branch by type, and use enable_if when you need separate overloads. C++20 concepts replace most of this later, but ROS 2 C++17 code still uses SFINAE.
Guided lesson
Work through these steps
- 1.
Read the sfinae basics example and identify what each line owns or changes.
- 2.
Create a small source file, compile with -std=c++17 and warnings enabled, and run it.
- 3.
Change one input, predict the result before running, then compare the output.
- 4.
Record one compiler or runtime error and explain the correction in your own words.
Worked example
Read it before you run it
#include <type_traits>
// Header for enable_if_t and the traits.
template <typename T>
// Overload 1: whole numbers only.
std::enable_if_t<std::is_integral_v<T>, const char*> kind(T) { return "integer"; }
// The return type exists only when T is integral; otherwise this overload disappears.
template <typename T>
// Overload 2: fractions only.
std::enable_if_t<std::is_floating_point_v<T>, const char*> kind(T) { return "floating"; }
// The return type exists only when T is a float type.
std::cout << kind(3) << '\n';
// Only overload 1 is viable: prints integer.
std::cout << kind(2.5) << '\n';
// Only overload 2 is viable: prints floating.
// kind(std::string{});
// No overload is viable, so this would not compile, with a clear 'no matching function' error.What the example is doing
- 1.Read declarations from right to left: identify each value's type, name, and initial value.
- 2.Follow the data through each expression before considering syntax shortcuts.
- 3.Compile with warnings enabled; a clean build is part of the result, not an optional polish step.
Watch it run · Values in memory
See what this code does
The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.
Demo for SFINAE basics. Expected result: integer floating
Practice code
Now type it yourself
Copy the starter into practice.cpp and replace every TODO with your own code. Build and run it: g++ -std=c++17 -Wall -Wextra -Wpedantic practice.cpp -o practice -pthread && ./practice
#include <iostream>
#include <type_traits>
// TODO: write two kind() overloads that use enable_if_t: one for integral types ("integer"), one for floating-point ("floating")
int main() {
std::cout << kind(3) << ' ' << kind(2.5) << '\n';
return 0;
}Expected output
integer floatingStuck? Show the full solution (try it yourself first)
#include <iostream>
#include <type_traits>
template <typename T>
std::enable_if_t<std::is_integral_v<T>, const char*> kind(T) {
return "integer";
}
template <typename T>
std::enable_if_t<std::is_floating_point_v<T>, const char*> kind(T) {
return "floating";
}
int main() {
std::cout << kind(3) << ' ' << kind(2.5) << '\n';
return 0;
}Expected result
How you know it worked
- After adding the shown fragment to the guided exercise with its listed headers and surrounding program, the finished program builds with -Wall -Wextra -Wpedantic.
- Changing the stated input produces the result you predicted.
Practical exercise
Apply the lesson
Write serialize(T) with one overload for arithmetic types (to_string) and one for types that have a .to_string() method, detected with a small trait. Then rewrite the arithmetic/other split with if constexpr and compare readability.
Before you continue
- The program builds with -std=c++17 and no warnings
- You can explain the key rule without reading the example
- The normal and edge cases both have recorded results
If something goes wrong
Start at the first error. Check spelling, required #include lines, and whether the declaration appears before use.
Print the inputs and intermediate values, then check conversions, units, and collection order.