title: C++11 style SFINAE and function visibility on template instantiation
body: <p>I'm not sure if this has anything to do with sfinae, or just something thats relevant for any templated function. I am attempting to use sfinae to enable/disable a member function based on existence of corresponding free function, which in turn is enabled/disabled based on existance of member function in another type, all using method described <a href="https://stackoverflow.com/a/9154394/1554020">here</a>:</p>
  
  <pre><code>struct S;
  
  template &lt;typename T&gt;
  inline auto f(S&amp; s, T const&amp; t)
     -&gt; decltype(t.f(s), void())
  {
     t.f(s);
  }
  
  struct S
  {
      template &lt;typename T&gt;
      auto f(T const&amp; t)
          -&gt; decltype(f(*this, t), void())
      {
          f(*this, t); // &lt;------------------------------------------- HERE
      }
  };
  
  struct pass
  {
      void f(S&amp;) const
      {
          //...
      }
  };
  
  struct fail
  {
  };
  
  int main()
  {
      S s;
      s.f(pass()); // should compile fine
      //s.f(fail()); // should fail to compile due to absence of f from S
      return 0;
  }
  </code></pre>
  
  <p>however gcc 4.7.1 gives me this on the line marked by arrow:</p>
  
  <blockquote>
    <p>error: no matching function for call to 'S::f(S&amp;, const pass&amp;)'
    <br> note: candidate is:
    <br> note: template decltype ((f((* this), t), void())) S::f(const T&amp;)
    <br> note:   template argument deduction/substitution failed:
    <br> note:   candidate expects 1 argument, 2 provided</p>
  </blockquote>
  
  <p>which apparently means that global <code>f</code> above is not considered for overload resolution.</p>
  
  <p>Why is that and what do I do to make it do so?</p>
  
  <p>Also why are there no errors two lines above that, where <code>f</code> used in decltype in similar fashion?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>As @n.m. said, member functions completely shadow free functions, even when their signatures are different, so here is a workaround that doesn't break ADL for <code>f</code> (unlike the full name qualification suggested by @n.m. ). Make free function (<code>f_dispatcher</code>) somewhere nobody will look (<code>detail</code>), and fully qualify its name inside <code>S::f</code>. In that function call free <code>f</code> and let ADL take care of it from there onwards, like so:</p>
  
  <pre><code>struct S;
  
  template &lt;typename T&gt;
  inline auto f(S&amp; s, T const&amp; t)
      -&gt; decltype(t.f(s), void())
  {
      t.f(s);
  }
  
  namespace detail
  {
      template &lt;typename T&gt;
      inline auto f_dispatcher(S&amp; s, T const&amp; t)
          -&gt; decltype(f(s, t), void())
      {
          f(s, t);
      }
  }
  
  struct S
  {
      template &lt;typename T&gt;
      auto f(T const&amp; t)
          -&gt; decltype(detail::f_dispatcher(*this, t), void())
      {
          detail::f_dispatcher(*this, t);
      }
  };
  
  struct pass
  {
      void f(S&amp;) const
      {
          //...
      }
  };
  
  struct fail
  {
  };
  
  int main()
  {
      S s;
      s.f(pass()); // compiles fine
      //s.f(fail()); // fails to compile due to absence of f from S
      return 0;
  }
  </code></pre>