How it works...

In this recipe, you will learn how to incorporate SFINAE in your own code. To start, we must first understand what SFINAE is and how the standard library uses it to implement type traits. Without knowing how type traits are implemented, it can be difficult to understand how to use them.

To start, the most important thing to understand with SFINAE is what its name says, which is that a substitution failure is not an error. What this means is that when a template type is being substituted, if a failure occurs, the compiler will not generate an error as a result. For example, we can write the following:

#include <iostream>struct the_answer{    using type = unsigned;};template<typename T>void foo(typename T::type t){ std::cout << ...

Get Advanced C++ Programming Cookbook 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.