How Does std::enable_if work?

12,377

Solution 1

As is mentioned in comment by 40two, understanding of Substitution Failure Is Not An Error is a prerequisite for understanding std::enable_if.

std::enable_if is a specialized template defined as:

template<bool Cond, class T = void> struct enable_if {};
template<class T> struct enable_if<true, T> { typedef T type; };

The key here is in the fact that typedef T type is only defined when bool Cond is true.

Now armed with that understanding of std::enable_if it's clear that void foo(const T &bar) { isInt(bar); } is defined by:

template<typename T>
typename std::enable_if<std::numeric_limits<T>::is_integer, void>::type foo(const T &bar) { isInt(bar); }

As mentioned in firda's answer, the = 0 is a defaulting of the second template parameter. The reason for the defaulting in template<typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0> is so that both options can be called with foo< int >( 1 );. If the std::enable_if template parameter was not defaulted, calling foo would require two template parameters, not just the int.


General note, this answer is made clearer by explicitly typing out typename std::enable_if<std::numeric_limits<T>::is_integer, void>::type but void is the default second parameter to std::enable_if, and if you have enable_if_t is a defined type and should be used. So the return type should condense to: std::enable_if_t<std::numeric_limits<T>::is_integer>

A special note for users of prior to : Default template parameters aren't supported, so you'll only be able to use the enable_if on the function return: std::numeric_limits as a Condition

Solution 2

template<typename T, std::enable_if<std::is_integral<T>::value, int>::type = 0>
void foo(const T& bar) { isInt(); }

this fails to compile if T is not integral (because enable_if<...>::type won't be defined). It is protection of the function foo.The assignment = 0 is there for default template parameter to hide it.

Another possibility: (yes the typename is missing in original question)

#include <type_traits>

template<typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0>
void foo(const T& bar) {}

template<typename T>
typename std::enable_if<std::is_integral<T>::value>::type
bar(const T& foo) {}

int main() {
    foo(1); bar(1);
    foo("bad"); bar("bad");
}
error: no matching function for call to ‘foo(const char [4])’
  foo("bad"); bar("bad");
           ^
note: candidate is:
note: template::value, int>::type  > void foo(const T&)
 void foo(const T& bar) {}
      ^
note:   template argument deduction/substitution failed:
error: no type named ‘type’ in ‘struct std::enable_if’
 template::value, int>::type = 0>
                                                                                       ^
note: invalid template non-type parameter
error: no matching function for call to ‘bar(const char [4])’
  foo("bad"); bar("bad");
                       ^
Share:
12,377

Related videos on Youtube

Jonathan Mee
Author by

Jonathan Mee

Software Engineer who architects key features designed for C++14 with defined unit tests, and who manages engineering teams by establishing achievable milestones and reporting demonstrable progress.

Updated on June 12, 2022

Comments

  • Jonathan Mee
    Jonathan Mee almost 2 years

    I just asked this question: std::numeric_limits as a Condition

    I understand the usage where std::enable_if will define the return type of a method conditionally causing the method to fail to compile.

    template<typename T>
    typename std::enable_if<std::numeric_limits<T>::is_integer, void>::type foo(const T &bar) { isInt(bar); }
    

    What I don't understand is the second argument and the seemingly meaningless assignment to std::enable_if when it's declared as part of the template statement, as in Rapptz answer.

    template<typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0>
    void foo(const T& bar) { isInt(); }
    
    • T.C.
      T.C. over 9 years
      (You are missing a typename in the second block of code you quoted.) typename std::enable_if<std::is_integral<T>::value, int>::type is int if the value is true, so it declares a non-type template parameter of type int with a default value of 0; if value is false, you get substitution failure so the overload is removed from consideration.
    • 101010
      101010 over 9 years
      In order to understand how std::enable_if works you'll have to understand how SFINAE works.
    • Jonathan Mee
      Jonathan Mee over 9 years
      @T.C. so it's trying to do an assignment at compile time and if it is assigning to an int the assignment succeeds?
    • Admin
      Admin over 9 years
      @JonathanMee It's not assignment.
    • Johan Råde
      Johan Råde over 9 years
      Here is good explanation of enable_if, boost.org/doc/libs/1_55_0/libs/utility/enable_if.html. It covers boost::enable_if, but std::enable_if works the same way.
    • Jonathan Mee
      Jonathan Mee over 9 years
      @user763305 I've read through the boost documentation and it really helped me understand what's going down here. I was planning on writing a synopsis of it for an answer here as time permits. I will accept your answer instead though if you feel like writing a synopsis first.
    • Johan Råde
      Johan Råde over 9 years
      I'm glad you found my comment useful. I don't have time to write a full answer; please go ahead and do it yourself.
    • Jonathan Mee
      Jonathan Mee over 9 years
      @user763305 I've gone ahead and scribbled up an answer, I'll accept it in a couple days, if you think it could use clarification, please feel free to edit it.
    • gedamial
      gedamial over 6 years
      @T.C. Why, in the implementation of std::enable_if, the template parameter T is defaulted to void? Isn't it the same without?
    • Jonathan Mee
      Jonathan Mee over 6 years
      @gedamial Yes in the case where the type returned will be void, the second template argument to enable_if may be omitted. The case I asked a question about illustrated passing a integral value, not a type, as a template argument. And defaulting that argument to 0. So given the function definition: template<typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0> void foo(const T& bar) { isInt(); } I could call this function like: foo<int, 13>(1) or foo<int>(1) in the latter case the second template argument would be defaulted to 0.
    • gedamial
      gedamial over 6 years
      @JonathanMee thank for the answer. then why is it important, in the implementation of std::enable_if, that the parameter T is defaulted to void (in the case the bool Condition is false)? If the condition is false, the function will be just hidden... there's no need to write typename T = void
    • Jonathan Mee
      Jonathan Mee over 6 years
      @gedamial If you're intensely curious as to the motivation for defaulting it might warrant a follow up question. As it is though it does let us write things a bit more simply. For example I can rewrite my first example in this question to something as simple as: template <typename T> enable_if_t<is_integral_v<T>> foo(const T& bar) { isInt(bar); } If you do ask the question though, add a link here, if there were any good motivation rationales I'd like to read them.