(""); } cl...">

"reference to local variable declared in enclosing function" linked with template instantiation · Issue #140622 · llvm/llvm-project (original) (raw)

template struct L { int operator()(const char*) const { return 1; } }; template constexpr L l;

int main() { constexpr auto LOCAL = "hello"; LOCAL, l(""); }

clang++ -std=c++23

<source>:10:3: error: reference to local variable 'LOCAL' declared in enclosing function 'main'
   10 |   LOCAL, l<float>("");
      |   ^
<source>:10:10: note: in instantiation of variable template specialization 'l<float>' requested here
   10 |   LOCAL, l<float>("");
      |          ^
<source>:9:18: note: 'LOCAL' declared here
    9 |   constexpr auto LOCAL = "hello";
      |                  ^
<source>:10:3: warning: left operand of comma operator has no effect [-Wunused-value]
   10 |   LOCAL, l<float>("");
      |   ^~~~~
1 warning and 1 error generated.
Compiler returned: 1

Tested with clang 20, clang trunk, see https://godbolt.org/z/hhxhqrxbo. GCC accepts it.