llvm-project/clang/test/PCH/instantiate-used-constexpr-function.cpp
cor3ntin 030047c432
[Clang] Eagerly instantiate used constexpr function upon definition. (#73463)
Despite CWG2497 not being resolved, it is reasonable to expect the
following code to compile (and which is supported by other compilers)

```cpp
  template<typename T> constexpr T f();
  constexpr int g() { return f<int>(); } // #1
  template<typename T> constexpr T f() { return 123; }
  int k[g()];
  // #2
```

To that end, we eagerly instantiate all referenced specializations of
constexpr functions when they are defined.

We maintain a map of (pattern, [instantiations]) independent of
`PendingInstantiations` to avoid having to iterate that list after each
function definition.

We should apply the same logic to constexpr variables, but I wanted to
keep the PR small.

Fixes #73232
2023-11-30 08:45:05 +01:00

18 lines
357 B
C++

// RUN: %clang_cc1 -std=c++2a -emit-pch %s -o %t
// RUN: %clang_cc1 -std=c++2a -include-pch %t -verify %s
// expected-no-diagnostics
#ifndef HEADER
#define HEADER
template<typename T> constexpr T f();
constexpr int g() { return f<int>(); } // #1
#else /*included pch*/
template<typename T> constexpr T f() { return 123; }
int k[g()];
#endif // HEADER