title: Template specialization to check whether a member is present
body: <p>This is a follow up to my question <a href="https://stackoverflow.com/questions/32636275/sfinae-with-variadic-templates">SFINAE with variadic templates</a>. I was trying to construct a variadic template named <code>has_member</code> for checking whether a class has a member (called <code>member</code>).</p>
  
  <p>I tried to see if I could use <code>size_t</code> instead of <code>void</code> in the answer to that question and not use variadic templates, as follows:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  using namespace std;
  
  class ClassWithMember
  {
      public:
      int member;
  };
  class ClassWithoutMember
  {
  };
  
  template &lt;typename T,typename=size_t&gt;
  class has_member: public std::false_type
  {
  };
  template &lt;typename T&gt;
  class has_member&lt;T,decltype(sizeof(typename T::member))&gt;: public std::true_type
  {
  };
  
  template &lt;typename T1,
            std::enable_if_t&lt;has_member&lt;T1&gt;::value&gt;* = nullptr&gt;
  void my_function(const T1&amp; obj) { cout&lt;&lt;"Function for classes with member"&lt;&lt;endl; }
  
  template &lt;typename T1,
            std::enable_if_t&lt;!has_member&lt;T1&gt;::value&gt;* = nullptr&gt;
  void my_function(const T1&amp; obj) { cout&lt;&lt;"Function for classes without member"&lt;&lt;endl; }
  
  
  int main()
  {
      ClassWithMember objWithMember;
      ClassWithoutMember objWithoutMember;
      my_function (objWithMember);
      my_function (objWithoutMember);
  }
  </code></pre>
  
  <p>I get no errors, but the specialization is never used. Only the function for classes without the member gets called. Could someone please explain why?</p>