title: std::enable_if to conditionally compile a member function
body: <p>I am trying to get a simple example to work to understand how to use <code>std::enable_if</code>. After I read <a href="https://stackoverflow.com/questions/6627651/enable-if-method-specialization/6627748#6627748">this answer</a>, I thought it shouldn't be too hard to come up with a simple example. I want to use <code>std::enable_if</code> to choose between two member-functions and allow only one of them to be used.</p>
  
  <p>Unfortunately, the following doesn't compile with gcc 4.7 and after hours and hours of trying I am asking you guys what my mistake is.</p>
  
  <pre><code>#include &lt;utility&gt;
  #include &lt;iostream&gt;
  
  template&lt; class T &gt;
  class Y {
  
      public:
          template &lt; typename = typename std::enable_if&lt; true &gt;::type &gt;
          T foo() {
              return 10;
          }
          template &lt; typename = typename std::enable_if&lt; false &gt;::type &gt;
          T foo() {
              return 10;
          }
  
  };
  
  
  int main() {
      Y&lt; double &gt; y;
  
      std::cout &lt;&lt; y.foo() &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>gcc reports the following problems:</p>
  
  <pre><code>% LANG=C make CXXFLAGS="-std=c++0x" enable_if
  g++ -std=c++0x    enable_if.cpp   -o enable_if
  enable_if.cpp:12:65: error: `type' in `struct std::enable_if&lt;false&gt;' does not name a type
  enable_if.cpp:13:15: error: `template&lt;class T&gt; template&lt;class&gt; T Y::foo()' cannot be overloaded
  enable_if.cpp:9:15: error: with `template&lt;class T&gt; template&lt;class&gt; T Y::foo()'
  </code></pre>
  
  <p>Why doesn't g++ delete the wrong instantiation for the second member function? According to the standard, <code>std::enable_if&lt; bool, T = void &gt;::type</code> only exists when the boolean template parameter is true. But why doesn't g++ consider this as SFINAE? I think that the overloading error message comes from the problem that g++ doesn't delete the second member function and believes that this should be an overload.</p>