title: Calling a member function if it exists, falling back to a free function and vice-versa
body: <p>Can I write a template function taking an argument T that calls a member function <code>foo</code> if it exists on <code>T</code>, and if it doesn't calls a free function <code>foo(T)</code> instead (and fails to compile if neither exists)?</p>
  
  <p>Something like:</p>
  
  <pre><code>template&lt;typename T&gt;
  int call_foo(T t) {
  // if T::foo() exists
      return t.foo();
  // else return foo(t);
  }
  </code></pre>
  
  <p>How about the reverse case: preferring a free function <code>foo</code> before the member function? I cannot use any features introduced after C++11.</p>