Substitution Failure Is Not An Error

The rule that a substitution failure arising from an expression that would be invalid with the specified or deduced types does not make the whole program invalid is known as Substitution Failure Is Not An Error (SFINAE). This rule is essential for using template functions in C++; without SFINAE, it would be impossible to write many otherwise perfectly valid programs. Consider this template overload, which differentiates between regular pointers and member pointers:

template <typename T> void f(T* i) {            // 1    std::cout << "f(T*)" << std::endl;}template <typename T> void f(int T::* p) {      // 2    std::cout << "f(T::*)" << std::endl;}struct A {    int i;};A a;f(&a.i);    // 1f(&A::i);   // 2

So far, so good—the first time, ...

Get Hands-On Design Patterns with C++ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.