<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/111266>111266</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [clang] crashes and misbehavior with member function template explicit specialization
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            bug,
            clang,
            c++,
            crash-on-valid,
            extension:microsoft,
            diverges-from:msvc,
            diverges-from:edg
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
            mizvekov
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          mizvekov
      </td>
    </tr>
</table>

<pre>
    Clang crashes compiling the following valid example: https://godbolt.org/z/Kx916caT7
```C++
template<class T> struct S {
 template<int> auto foo();
  template<> auto foo<1>() {
    return [](auto x) { return x; };
  }
};
template struct S<void>;
```

What happens is that when instantiating the members of S<int>, we lose track of the specialization kind of `foo<1>`, leading to us using the explicit specialization argument when instantiating the function definition.

The behavior for explicit instantiation doesn't match MSVC and EDG as well: https://godbolt.org/z/ejehb88zc
```C++
template<class T> struct A {
  int x;
  template<class U = void> static void f();
 template<> void f<int>() { (void)this->x; }
};
template struct A<int>;
```
Here clang eagerly instantiates `f<int>` and complains about usage of `this` in static function, while MSVC / EDG only do so when the definition is actually used.

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyclUGPszYTxz_N5DJKBCYhcOCQTTbvK1U9Pdv2bMwAftbYkW2S7H76ygQIXXXVqhJKGPvvGXvm54E7JxtNVMDuBRjr5OeV3s0VGIPdacV73xpbTKOr0lQfxVFx3aCw3LXkUJjuIpXUDfqWsDZKmVuwrlzJCunOu4siSA7Yen9xkByAnYGdG1OVRvmNsQ2w8yew8y_3PE4Ff9tDdILoAGn0eI7AXsIzjHrqLop7guQoFHcO3yB5RedtLzz-QNiPOlwIpfZBxHtvsDYGWAYsh2RSLqV_0SXHGJLXh3zhGREt-d5qDCnbnYBlw5L7KJtm75C8IOxPi0jBehxuMTyFn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