title: C++ compile time check if method exists in template type
body: <p>I have a template that calls a member function. How do I check with <code>static_assert</code> that the method exists?</p>
  
  <pre><code>struct A {
  
  };
  
  struct B {
      int foo() { return 42; } };
  
  template &lt;typename T&gt; struct D {
      static_assert(/* T has foo */, "T needs foo for reasons");
  
      int bar() {
         return t.foo();
      }
  
      T t; };
  
  int main() {
      D&lt;A&gt; d;
  
      std::cout &lt;&lt; d.bar() &lt;&lt; std::endl;
  
      return 0; }
  </code></pre>
  
  <p>I know this will just generate a compiler error that A does not have foo but I would like to check and give a better error output using <code>static_assert</code>.</p>