I am encountering a linker error with Clang 19.1.4 when using lambdas as non‐type template parameters across multiple translation units. The minimal example below compiles without issues using GCC and also does not trigger the link error in Clang when optimizations (-O) are enabled or if I change the parameter from const auto& to auto (i.e., pass by value rather than by const reference).
Error:
`.rodata._ZTAXtl3$_0EE' referenced in section `.text' of pass_b.o: defined in discarded section `.rodata._ZTAXtl3$_0EE[_ZTAXtl3$_0EE]' of pass_b.o
(_ZTAXtl3$_0EE demangles to template parameter object for $_0{}.)
Command to reproduce:
clang++ -std=c++20 pass_a.cpp pass_b.cpp
Example code:
pass.h
#ifndef PASS_H
#define PASS_H
void PassArg(const auto& arg)
{
}
template<auto object>
void PassTemplate()
{
PassArg(object);
}
#endif
pass_a.cpp
#include "pass.h"
constexpr auto fn_a = []{};
void pass_a()
{
PassTemplate<fn_a>();
}
int main(int, char**)
{
return 0;
}
pass_b.cpp
#include "pass.h"
constexpr auto fn_b = []{};
void pass_b()
{
PassTemplate<fn_b>();
}