title: Detect operator support with decltype/SFINAE
body: <p>A (somewhat) outdated <a href="http://blog.think-async.com/2009/07/user-friendly-compile-errors-for.html">article</a> explores ways to use <code>decltype</code> along with SFINAE to detect if a type supports certain operators, such as <code>==</code> or <code>&lt;</code>.  </p>
  
  <p>Here's example code to detect if a class supports the <code>&lt;</code> operator:</p>
  
  <pre><code>template &lt;class T&gt;
  struct supports_less_than
  {
      static auto less_than_test(const T* t) -&gt; decltype(*t &lt; *t, char(0))
      { }
  
      static std::array&lt;char, 2&gt; less_than_test(...) { }
  
      static const bool value = (sizeof(less_than_test((T*)0)) == 1);
  };
  
  int main()
  {
      std::cout &lt;&lt; std::boolalpha &lt;&lt; supports_less_than&lt;std::string&gt;::value &lt;&lt; endl;
  }
  </code></pre>
  
  <p>This outputs <code>true</code>, since of course <code>std::string</code> supports the <code>&lt;</code> operator.  However, if I try to use it with a class that <em>doesn't</em> support the <code>&lt;</code> operator, I get a compiler error:</p>
  
  <pre><code>error: no match for ‘operator&lt;’ in ‘* t &lt; * t’
  </code></pre>
  
  <p>So SFINAE is not working here.  I tried this on GCC 4.4 and GCC 4.6, and both exhibited the same behavior.  So, is it possible to use SFINAE in this manner to detect whether a type supports certain expressions?  </p>