title: SFINAE: Detecting existence of member variable does not work on g++
body: <p>I'm trying to combine the approaches used in <a href="https://stackoverflow.com/a/16000226/4792865">this answer</a> for detecting whether a class has a member variable <code>x</code> with <a href="https://stackoverflow.com/a/47935516/4792865">this answer</a> to select different implementations depending on that using <code>enable_if</code>.</p>
  
  <p>Basically, I want to write a trait class that, given a type <code>T</code>, provides access to the member <code>T::x</code> if it exists, and provides a default value otherwise.</p>
  
  <p>The following code does not compile on g++: (<a href="https://godbolt.org/z/K24_r5" rel="noreferrer">Compiler Explorer</a>)</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt;
  #include &lt;type_traits&gt;
  
  // classes with / without x member
  struct WithX { static constexpr int x = 42; };
  struct WithoutX {};
  
  // trait to detect x
  template &lt;typename T, typename = void&gt;
  struct HasX : std::false_type { };
  
  template &lt;typename T&gt;
  struct HasX &lt;T, decltype((void) T::x)&gt; : std::true_type { };
  
  // trait to provide default for x
  template &lt;typename T&gt;
  struct FooTraits
  {
      template &lt;bool enable = HasX&lt;T&gt;::value&gt;
      static constexpr std::enable_if_t&lt; enable, size_t&gt; x() { return T::x; }
      template &lt;bool enable = HasX&lt;T&gt;::value&gt;
      static constexpr std::enable_if_t&lt;!enable, size_t&gt; x() { return 1; }
  
  };
  
  
  int main() {
      std::cout &lt;&lt; HasX&lt;WithX&gt;::value &lt;&lt; std::endl;
      // Uncomment the following line to make this compile with g++
      //std::cout &lt;&lt; HasX&lt;WithoutX&gt;::value &lt;&lt; std::endl;
      std::cout &lt;&lt; FooTraits&lt;WithoutX&gt;::x() &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>g++ gives error messages that</p>
  
  <pre><code>error: 'x' is not a member of 'WithoutX'
    struct HasX &lt;T, decltype((void) T::x)&gt; : std::true_type { };
  </code></pre>
  
  <p>in the part which should detect whether <code>x</code> is a member in the first place. Curiously though, if I uncomment the second to last line which instantiates <code>HasX&lt;WithoutX&gt;::value</code> by itself, g++ compiles without errors (<a href="https://godbolt.org/z/r0zD5u" rel="noreferrer">Compiler Explorer</a>).</p>
  
  <p>Both clang and msvc compile without a problem on Compiler Explorer.</p>
  
  <p>What's wrong here?</p>