Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Memory and Utilities

C++ std::enable_if and SFINAE

Reference for std::enable_if and SFINAE: enable or disable templates by condition, with return-type and default-argument forms, and the C++20 alternatives.

std::enable_if (header <type_traits>) is a trait that either has a member type or has none, depending on a compile-time condition. std::enable_if<Cond, T>::type is T when Cond is true and does not exist when Cond is false. Combined with SFINAE ("substitution failure is not an error"), a missing type does not cause a hard error: the compiler silently drops that template from overload resolution and tries others.

That makes enable_if the classic C++11 tool for "this overload only exists for integers" or "this class specialization applies to pointers". In modern code (C++20), concepts and requires clauses express the same intent more clearly and with better error messages, and if constexpr often removes the need for overloads altogether.

C++
template <class T>
std::enable_if_t<std::is_integral_v<T>, T> twice(T x) { return x * 2; }

Try it

C++ Overloads selected by enable_if

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Class specialization and the C++20 way

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
template<bool B, class T = void> struct enable_if;   // has ::type == T only if B is true
template<bool B, class T = void>
using enable_if_t = typename enable_if<B, T>::type;  // C++14
Name Description
B The compile-time condition, usually a trait such as std::is_integral_v<T>.
T The type produced when the condition holds. Defaults to void.

Returns: the nested type type only when B is true; otherwise there is no member and substitution fails (which is not an error inside overload resolution).

Errors / undefined behavior: if no overload remains, the call fails to compile with a long "no matching function" message. Specializing enable_if yourself is undefined behavior.

Three common placements:

C++
template <class T> std::enable_if_t<COND, T> f(T);                 // in the return type
template <class T, std::enable_if_t<COND, int> = 0> void f(T);     // as a defaulted non-type parameter
template <class T, class = std::enable_if_t<COND>> void f(T);      // as a default type argument (pitfall below)

Usage notes

  • Prefer the non-type parameter form (enable_if_t<COND, int> = 0). With a default type argument, two overloads that differ only in the enable_if condition are redeclarations of the same template and do not compile.
  • SFINAE only applies in the immediate context of the declaration. Errors inside a function body or a nested template instantiation are hard errors.
  • Conditions must be mutually exclusive (or ordered) for overloads; if two overloads both match, the call is ambiguous.
  • Constructors: enable_if goes in a defaulted template parameter because constructors have no return type.
  • Replace it with if constexpr when one function can branch internally: no overloads and no SFINAE. The branches not taken are not instantiated.
  • Replace it with concepts in C++20: template <std::integral T> or requires std::is_integral_v<T>. Constraint failures name the unmet requirement.
  • void_t and detection idiom are the companion technique for "does this expression compile?" queries.
  • Readability costs: enable_if nests quickly; wrap repeated conditions in a named constexpr bool or a variable template.
  • Version notes: enable_if since C++11, enable_if_t since C++14.