https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87377
Bug ID: 87377 Summary: error with generic lambda accessing static field through argument within return type Product: gcc Version: 9.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: comexk at gmail dot com Target Milestone: --- …that's a mouthful. Here's the code: template <auto _val> struct const_val { static constexpr auto val = _val; }; int main() { auto lambda = [](auto i) -> const_val<i.val> { while (1); }; lambda(const_val<5>()); } Currently, GCC trunk rejects this: % /tmp/gcct/bin/g++ b.cpp -std=c++2a b.cpp: In function ‘int main()’: b.cpp:6:48: error: template argument 1 is invalid 6 | auto lambda = [](auto i) -> const_val<i.val> { while (1); }; | However, Clang accepts it. I think this is a bug in GCC, because GCC does accept i.val as a constant expression within the function body; for example, the example compiles if the definition of 'lambda' is changed to: auto lambda = [](auto i) { const_val<i.val> c; while (1); }; It also accepts this: auto lambda = [](auto i) -> const_val<decltype(i)::val> { while (1); };