https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128567
Bug ID: 128567
Summary: Wrong static initializer emitted
Product: gcc
Version: 16.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: jeanmichael.celerier at gmail dot com
Target Milestone: ---
Wrong static initializer emitted for array of structs with default member
initializers built by an immediately-invoked lambda.
Elements starting from the modified one lose their initialization except for
values explicitely set.
Repro (from gcc 9 to 16, gcc 8 doesn't have the issue):
#include <cstdio>
struct Role { int duty{2}, level{12}, rate{4}; };
struct Arr { Role e[5]; };
Arr g = [] { Arr r{}; r.e[2].level = 1; return r; }();
int main() {
for (auto& r : g.e)
printf("%d/%d/%d ", r.duty, r.level, r.rate);
puts("");
}
Expected: 2/12/4 2/12/4 2/1/4 2/12/4 2/12/4
Actual: 2/12/4 2/12/4 0/1/0 0/0/0 0/0/0
Seems to trigger starting from -std=c++17, not before.
https://gcc.godbolt.org/z/59b5T9Mn1