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

    <tr>
        <th>Summary</th>
        <td>
            Clang instantiates function templates inside a discarded statement
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang
      </td>
    </tr>

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

    <tr>
      <th>Reporter</th>
      <td>
          seha-bot
      </td>
    </tr>
</table>

<pre>
    According to [[temp.inst]/5](https://eel.is/c++draft/temp.inst#5), a function template will be instantiated if its definition is required or if it changes the semantics of the program. A definition is required for a function if its name is odr-used outside a discarded statement [[basic.def.odr]/12](https://eel.is/c++draft/basic.def.odr#12.sentence-1).

However, if we write code such as:

```c++
template <typename T>
int f() {
    T *ptr;
    return 0;
}

int main() {
    if constexpr (false) {
 int x = f<int &>();  // error
    }
}
```

clang attempts to instantiate `f<int &>` which forms an invalid type and the compilation fails. The instantiation should not have happened in the first place. `f<int &>` is inside a discarded statement, so its definition is not required.

Reproduction: https://godbolt.org/z/Y4M5vWcf8
There is one more example in that link which passes correctly just to show that this behavior is also inconsistent in clang. This is a regression because it worked fine in earlier versions of clang as shown in that link.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyUVE1v4zYQ_TX0ZRBBoixLPujgJDV66aUIUPQ4IkcWdylS5VD2pr--IOV0k_0oUMCwIOrNx3t8M8hsLo6oF82jaJ53uMbJh55pwofBx93g9Wt_UsoHbdwFoocMfIw0L4VxHEXzLOS5yY9uinFhUZ-EPAt5JrKFYSHPSshHIR91wDEKef4aK-tGyKOQT4Awrk5F4x2kzxYjwc1YCwNBgqKLBiNpMCOYyKBpNM5kvGEI9NdqAmnwYQOAmtBdiCFOBExzClcMfswHS_CXgHMBp5_lGX1439K9qMOZEszr8LByKrdGNpoAQRtWGDRp4IiRZnLxrtSAbFShaSy8Dptalfw_cn1MIOtKFkwuklP0UAl5LER5EuXpV3-j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