title: SFINAE: What is happening here?
body: <p>I am currently trying to understand a C++ code, and have come across SFINAE construct (which is new to me). I have created a minimal example, based on the code I am looking at below:</p>
  
  <pre><code>#include&lt;iostream&gt;
  
  /* ----------------------------------------------
   Define two kernels: characterized by their dimension
     ---------------------------------------------- */
  struct Kern2
  {
    static constexpr int dim = 2;
  };
  
  struct Kern3
  {
    static constexpr int dim = 3;
  };
  
  /* ----------------------------------------------
   Choose which function to evaluate based on 
   dimension of Kern (Kern::dim)
     ---------------------------------------------- */
  template&lt;class Kern,
           typename std::enable_if&lt;Kern::dim == 2, bool&gt;::type = true&gt;
  inline void apply_kern(){
    std::cout &lt;&lt; "dim=2" &lt;&lt; "\n";
  }
  
  template&lt;class Kern,
           typename std::enable_if&lt;Kern::dim == 3, bool&gt;::type = false&gt;
  inline void apply_kern(){
    std::cout &lt;&lt; "dim=3" &lt;&lt; "\n";
  }
  
  // Try to see if the above SFINAE construct works!
  int main()
  {
  
   apply_kern&lt;Kern2&gt;(); // should print 'dim=2'
   apply_kern&lt;Kern3&gt;(); // should print 'dim=3'
  
    return 0;
  }
  </code></pre>
  
  <p>This gives as output:</p>
  
  <pre><code>&gt; dim=2
  &gt; dim=3
  </code></pre>
  
  <p>which is exactly what it's supposed to do. However, I am unable to understand exactly <em>how</em> this works? In particular, it appears that the same output is created if I switch the </p>
  
  <pre><code>typename std::enable_if&lt;Kern::dim == 2, bool&gt;::type = true
  </code></pre>
  
  <p>lines to:</p>
  
  <pre><code>typename std::enable_if&lt;Kern::dim == 2, bool&gt;::type = false
  </code></pre>
  
  <p>So I'm wondering what the meaning of these is? If someone could kindly explain what's going on, I'd greatly appreciate it! I haven't been able to find this precise way to use SFINAE online, unfortunately.</p>
  
  <p>Thanks!</p>