https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110642
Bug ID: 110642 Summary: Undefined behavior in same constant expression is found not always Product: gcc Version: 13.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: fchelnokov at gmail dot com Target Milestone: --- This lambda auto f = [](int i) { const auto y = i; return [&] () { return y; }; }; returns another lambda that makes undefined behavior: it accesses a stack variable (y) after the end of its life. And it is successfully caught during constant expression evaluation by GCC and other compilers, if verified like this: constexpr auto g = f(3)(); static_assert( f(3)() == 3 ); Online demo: https://gcc.godbolt.org/z/3Y4cGMG15 But if one changes the order of lines: static_assert( f(3)() == 3 ); constexpr auto g = f(3)(); then GCC stops detecting undefined behavior. Online demo: https://gcc.godbolt.org/z/dojhKasPs Could you please review why this happens?