https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101566
Bug ID: 101566 Summary: gcc miscompiles lambda used as tuple-like object applied to function for call Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: weilercdale at gmail dot com Target Milestone: --- Created attachment 51191 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=51191&action=edit minimized test case The following code uses a compiler generated structure via a lambda and `[=]` capture-list to create a "tuple"-like object, `get<N>` is implemented in terms of the gcc provided intrinsic `__integer_pack(N)` to emulate `std::index_sequence` for peeling off the value from the "tuple"-like object. `apply` calls any callable by applying `get<>` to each value in the "tuple"-like object. `PackCount` exposes the parameter pack count of the "tuple"-like object created with `make`, A `decltype` of the lambda is used to create a new struct `Lambda` that inherits both, making `PackCount<Ts...>::ELEMENTS` available as a compile-time constant to each created "tuple"-like object. A `Tuple` type is provided by taking the `decltype` of `make` synthesizing values for the call with `declval`. The code compiles in gcc, msvc, and clang but only works in the latter two compilers, gcc appears to miscompile the code under -O3. When compiling in gcc with -fsanitize=undefined, the code isn't miscompiled, and it runs fine. Under other optimization levels the code isn't miscompiled either. This appears to affect multiple versions of gcc including 10.1, 10.2, 10.3, 11.1.0, 11.1.1, and current trunk as can be verified on compiler explorer here https://godbolt.org/z/3sYnrj6nW