Note that there are some explanatory texts on larger screens.

plurals
  1. POErrors using decltype() and SFINAE
    text
    copied!<p>In response to .. some other question somewhere, I wrote this code.</p> <pre><code>struct no_type{}; template&lt;typename T&gt; struct has_apply { static decltype(T().apply&lt;0u&gt;(double())) func( T* ptr ); static no_type func( ... ); static const bool result = !std::is_same&lt;no_type, decltype(func(nullptr))&gt;::value; }; class A { public: template&lt; unsigned n &gt; void apply( const double&amp; ); }; class B { }; int main() { std::cout &lt;&lt; std::boolalpha &lt;&lt; has_apply&lt; A &gt;::result &lt;&lt; '\n'; std::cout &lt;&lt; std::boolalpha &lt;&lt; has_apply&lt; B &gt;::result &lt;&lt; '\n'; std::cin.get(); return( 0 ); } </code></pre> <p>Now it seems to me that result should be true if T offers a non-static member function "apply" that accepts a double rvalue and a template parameter literal, and false otherwise. However, the example given actually fails to compile for class B, when compiling <code>has_apply&lt;B&gt;</code>. Shouldn't the fact that the substitution of T failed in the decltype statement mean that it simply calls the other func? Isn't that kind of the point of SFINAE?</p> <p>Solved in the most ridiculous, pointless fashion ever:</p> <pre><code>struct no_type{}; template&lt;typename T&gt; struct has_apply { template&lt;typename U&gt; static decltype(U().apply&lt;0u&gt;(double())) func( U* ); template&lt;typename U&gt; static no_type func( ... ); static const bool result = !std::is_same&lt;no_type, decltype(func&lt;T&gt;(nullptr))&gt;::value; }; class A { public: template&lt; unsigned n &gt; void apply( const double&amp; ); }; class B { }; int main() { std::cout &lt;&lt; std::boolalpha &lt;&lt; has_apply&lt; A &gt;::result &lt;&lt; '\n'; std::cout &lt;&lt; std::boolalpha &lt;&lt; has_apply&lt; B &gt;::result &lt;&lt; '\n'; std::cin.get(); return( 0 ); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload