title: Is it possible to use 'if constexpr' to determine if a template function could be instantiated with a particular type parameter?
body: <p>Suppose there exists the following code:</p>
  <pre class="lang-cpp prettyprint-override"><code>class Foo {
   public:
      void foo() const { std::cout &lt;&lt; &quot;foo&quot; &lt;&lt; std::endl; }
  };
  
  class Bar {
   public:
      void bar() const { std::cout &lt;&lt; &quot;bar&quot; &lt;&lt; std::endl; }
  }; 
  
  template &lt;typename T&gt;
  void DoFoo(const T&amp; f) {
      f.foo();
  }
  </code></pre>
  <p>I want to write a function like this:</p>
  <pre class="lang-cpp prettyprint-override"><code>template &lt;typename T&gt;
  void DoFooIfPossible() {
      if constexpr (/* DoFoo&lt;T&gt;(T()) would compile */) {
          DoFoo(T());
      } else {
          std::cout &lt;&lt; &quot;[not possible]&quot; &lt;&lt; std::endl;
      }
  }
  </code></pre>
  <p>So that:</p>
  <pre class="lang-cpp prettyprint-override"><code>int main() {
      DoFooIfPossible&lt;Foo&gt;();
      DoFooIfPossible&lt;Bar&gt;();
  }
  </code></pre>
  <p>compiles and prints:</p>
  <pre><code>foo
  [not possible]
  </code></pre>
  <p>I know that <strong>for this particular example</strong> I can implement this in the following way by detecting the presence of the member function that <code>DoFoo</code> uses:</p>
  <pre class="lang-cpp prettyprint-override"><code>template &lt;typename T, typename = void&gt;
  struct IsFooPossible : std::false_type {};
  
  template &lt;typename T&gt;
  struct IsFooPossible&lt;
      T, std::enable_if_t&lt;std::is_member_function_pointer_v&lt;decltype(&amp;T::foo)&gt;&gt;&gt; 
      : std::true_type {};
  
  template &lt;typename T&gt;
  void DoFooIfPossible() {
      if constexpr (IsFooPossible&lt;T&gt;::value) {
          DoFoo(T());
      } else {
          std::cout &lt;&lt; &quot;[not possible]&quot; &lt;&lt; std::endl;
      }
  }
  </code></pre>
  <p>However, the question I am asking here is: <strong>Can I implement this without making any assumptions about the implementation of <code>DoFoo</code>?</strong>.</p>
  <p>In a real-world scenario, <code>DoFoo</code> may be a library function that I do not own. It may place many different conditions on its template parameter type <code>T</code>, and those conditions may change over time. So replicating those conditions in an <code>enable_if</code> expression in my code is not a viable solution.</p>
  <p>I was wondering if it is possible to write my <code>if constexpr</code> expression in a way that <em>directly</em> tests whether <code>DoFoo&lt;T&gt;</code> can be instantiated without having any special knowledge of the implementation of <code>DoFoo</code>, and without modifying <code>DoFoo</code>.</p>
  <p>I'm trying to do this in C++17, so if there's something in C++20 that could handle this, that would be interesting to know but wouldn't solve my problem.</p>
  <p>(Also note that for the sake of creating a minimal example, I'm assuming <code>T</code> is default-constructable. I don't really care about the <code>T()</code> part of <code>DoFoo(T())</code> -- I'm trying to determine if <code>DoFoo&lt;T&gt;</code> can be instantiated at all.)</p>