Where can std::enable_if go in a C++ function template, and why do two overloads differing only in a defaulted enable_if template argument fail to compile?
answer
- no type member when false
- return, parameter, template parameter
- defaults are not the signature
- enable_if_t<Cond, int> = 0
basics
~20 sstd::enable_if<Cond, T>::type exists only when Cond is true, so it can sit in the return type, an extra parameter or a template parameter. Default template arguments are not part of a template's signature, so two templates differing only there are redeclarations.
solid answer
~50 s`std::enable_if<B, T>` has a member `type` (equal to `T`, default `void`) only when `B` is `true`; `std::enable_if_t` (C++14) is the alias. Naming that missing `type` in a signature triggers SFINAE, and there are three places to put it: the return type (`std::enable_if_t<C, std::string>`), an extra defaulted parameter (`std::enable_if_t<C>* = nullptr`), or a template parameter. The trap is the form `template <class T, class = std::enable_if_t<C>>`: the condition lives only in a **default argument**, which is not part of the template's signature, so a second overload with a different condition declares the same template and clang reports `template parameter redefines default argument`. Put the condition in the parameter's **type** instead, `std::enable_if_t<C, int> = 0`, and the two overloads become distinct. Constructors have no return type, so they use the template-parameter form (or, in older code, an extra defaulted parameter).
code
cpp · 21 lines#include <iostream>
#include <string>
#include <type_traits>
template <class T, std::enable_if_t<std::is_integral_v<T>, int> = 0>
std::string describe(T) { return "integer"; }
template <class T, std::enable_if_t<std::is_floating_point_v<T>, int> = 0>
std::string describe(T) { return "floating"; }
// Return-type form: the condition is part of the signature too.
template <class T>
std::enable_if_t<std::is_enum_v<T>, std::string> describe(T) { return "enum"; }
enum class Color { red };
int main() {
std::cout << describe(42) << ' ' << describe(2.5) << ' '
<< describe(Color::red) << '\n';
// describe("text"); // error: no matching function, every candidate removed
}go deeper
Know that enable_if has a type member only when its condition is true, and recognise the enable_if_t alias in a template signature.
Show the three placements, explain why the class = enable_if_t form collides, and write the int = 0 fix from memory.
Keep overload conditions mutually exclusive, pick placements that survive constructors and operators, and read clang's candidate-ignored notes to find which condition failed.
Decide a codebase convention for pre-C++20 constraints and when maintaining enable_if overload sets costs more than raising the language baseline.
## What std::enable_if is `std::enable_if` is a tiny class template in `<type_traits>`. The standard specifies it as: if `B` is `true`, the member typedef `type` denotes `T`; otherwise **there is no member `type`**. libc++ implements it exactly that way, with an empty primary template and a partial specialization for `true`: ```cpp template <bool, class T = void> struct enable_if {}; template <class T> struct enable_if<true, T> { typedef T type; }; template <bool B, class T = void> using enable_if_t = typename enable_if<B, T>::type; // C++14 ``` On its own it does nothing. It becomes useful only when `enable_if_t<B, T>` appears in a function template's signature, where a missing `type` is a **substitution failure** that removes the overload. ## The three placements | Placement | Example | Good for | Caveat | |---|---|---|---| | Return type | `std::enable_if_t<std::is_enum_v<T>, std::string> describe(T)` | ordinary functions | unusable for constructors and conversion operators; buries the real return type | | Extra parameter | `void f(T, std::enable_if_t<C>* = nullptr)` | constructors in C++03-style code | changes the function's arity and its function-pointer type | | Template parameter | `template <class T, std::enable_if_t<C, int> = 0>` | constructors, operators, everything | must be written as a non-type parameter, not a defaulted type | ## The redeclaration trap The most common mistake writes the condition as a defaulted **type** template parameter: ```cpp template <class T, class = std::enable_if_t<std::is_integral_v<T>>> std::string describe(T) { return "integer"; } template <class T, class = std::enable_if_t<std::is_floating_point_v<T>>> std::string describe(T) { return "floating"; } // error ``` Two function templates are the same template when their template-heads, parameter types and return types are equivalent. **Default template arguments are not part of that comparison.** Both declarations are therefore `template <class T, class> std::string describe(T)`, and the second is a redefinition. Apple clang 21 (`-std=c++17`) says `error: template parameter redefines default argument` followed by `error: redefinition of 'describe'`. ## The fix: put the condition in the type ```cpp template <class T, std::enable_if_t<std::is_integral_v<T>, int> = 0> std::string describe(T) { return "integer"; } template <class T, std::enable_if_t<std::is_floating_point_v<T>, int> = 0> std::string describe(T) { return "floating"; } ``` Now the second template parameter is a **non-type** parameter whose type is `enable_if_t<cond, int>`. The two conditions are different expressions, so the template-heads are not equivalent and the declarations are two distinct overloads. When the condition is true the parameter is an `int` defaulted to `0`; when it is false its type cannot be formed and the overload disappears. ## Choosing a placement - Prefer the **template-parameter form** with `int = 0` as the house default: it works for constructors, operators and functions alike and leaves the return type readable. - Use the **return-type form** when the function already returns a computed type and you want the condition next to it. - Avoid the **extra-parameter form** in new code: it leaks into the function type and conflicts with operators whose parameter count is fixed. - Keep conditions **mutually exclusive**. If two enabled overloads can both be viable for one type, the call is ambiguous, because `enable_if` adds no ranking. - Remember the helpers: `enable_if_t` arrived in C++14 and the `_v` variable templates such as `std::is_integral_v` in C++17; C++11 code writes `typename std::enable_if<std::is_integral<T>::value>::type`. ## Member functions of a class template A frequent surprise is enabling a member function on the **class's** parameter: ```cpp template <class T> struct Box { template <std::enable_if_t<std::is_integral_v<T>, int> = 0> // wrong T twice() const; }; ``` `T` is already fixed when `Box<double>` is instantiated, so the condition is not substituted during deduction of `twice`; it is evaluated as soon as the class's member declarations are instantiated, and the whole class fails. Apple clang 21 reports `no type named 'type' in 'std::enable_if<false, int>'; 'enable_if' cannot be used to disable this declaration`. The fix is to make the condition depend on a parameter of the member template itself: `template <class U = T, std::enable_if_t<std::is_integral_v<U>, int> = 0> U twice() const;`. ## What a caller sees on misuse When every overload is disabled, the compiler reports that no function matches. Apple clang 21 then lists each candidate with a note such as `candidate template ignored: requirement 'std::is_integral_v<const char *>' was not satisfied [with T = const char *]`, which is readable as long as the condition is a single trait. Longer conditions produce longer notes, which is one reason C++20 constraints replaced this technique for new code.
- Why does std::enable_if_t<C, int> = 0 avoid the redeclaration problem?The condition moves from a default argument into the type of a non-type template parameter. Template-heads compare parameter types, and `enable_if_t<std::is_integral_v<T>, int>` and `enable_if_t<std::is_floating_point_v<T>, int>` depend on different expressions, so the two templates are not equivalent and both declarations stand as separate overloads.
- What does the extra-parameter form, std::enable_if_t<C>* = nullptr, cost?It adds a real parameter to the function, so taking the function's address yields a pointer with an extra argument, and it cannot be used where the parameter count is fixed, such as most overloaded operators. It was popular before default template arguments for function templates arrived in C++11, and is rarely the best choice now.
- What happens if two enable_if overloads are both enabled for one type?Both survive substitution, and `std::enable_if` adds nothing that ranks one above the other, so if their parameters match equally well the call is ambiguous. SFINAE conditions must be written mutually exclusive, for example `is_integral_v<T>` and `!is_integral_v<T> && is_arithmetic_v<T>`, or the overloads must differ in some other way that ranks them.
- std::enable_if<false> still yields void, so the overload compiles.
- Two templates differing only in a default template argument are distinct overloads.
- enable_if can be used in a constructor's return type.
- A static_assert at the top of the body removes the overload like enable_if does.
- enable_if_t and enable_if::type behave differently under SFINAE.