Issue 140449
Summary Clang instantiates function templates inside a discarded statement
Labels clang
Assignees
Reporter seha-bot
    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.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to