Conditionally enable functions based on types with std::enable_if_t

The std::enable_if_t type trait is used for function overloading when dealing with a template function. While regular function overloading requires you to overload a function for every type you intend to use it with, std::enable_if_t uses a compile-time predicate for overloading.

Let's say we'd like to create an interpolate function, which mixes two values weighted by a power parameter from zero to one:

Illustration of interpolation function

As the power must be a decimal number, the function only works with floats and doubles. Using regular function overloading, we have to ...

Get C++ High Performance 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.