title: SFINAE decltype comma operator trick
body: <p>After reading Matthieu's answer <a href="https://stackoverflow.com/a/9531274/955273">here</a>, I decided to try this myself.</p>
  
  <p>My attempt fails to compile because SFINAE doesn't kick in and cull the <code>has_foo</code> function which attempts to access <code>T::foo</code>.</p>
  
  <pre><code>error: ‘struct Bar’ has no member named ‘foo’
  </code></pre>
  
  <p>Am I missing something, or is what I'm attempting to do not possible in this way?</p>
  
  <p>(I'm using gcc-4.7.2)</p>
  
  <p>Full examplar below:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  // culled by SFINAE if foo does not exist
  template&lt;typename T&gt;
  constexpr auto has_foo(T&amp; t) -&gt; decltype((void)t.foo, bool())
  {
      return true;
  }
  // catch-all fallback for items with no foo
  constexpr bool has_foo(...)
  {
      return false;
  }
  //-----------------------------------------------------
  
  template&lt;typename T, bool&gt;
  struct GetFoo
  {
      static int value(T&amp; t)
      {
          return t.foo;
      }
  };
  template&lt;typename T&gt;
  struct GetFoo&lt;T, false&gt;
  {
      static int value(T&amp;)
      {
          return 0;
      }
  };
  //-----------------------------------------------------
  
  template&lt;typename T&gt;
  int get_foo(T&amp; t)
  {
      return GetFoo&lt;T, has_foo(t)&gt;::value(t);
  }
  //-----------------------------------------------------
  
  struct Bar
  {
      int val;
  };
  
  int main()
  {
      Bar b { 5 };
      std::cout &lt;&lt; get_foo(b) &lt;&lt; std::endl;
      return 0;
  }
  </code></pre>