https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115768
Bug ID: 115768 Summary: [C23] constexpr array of string literals not optimized Product: gcc Version: 14.1.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: biggs at biggs dot xyz Target Milestone: --- Consider: https://godbolt.org/z/c9fME1qdh ```c #include <stdio.h> enum ColorEnum { CE_RED, CE_GREEN, CE_BLUE, }; static constexpr char names[][6] = { [CE_RED] = "RED", [CE_GREEN] = "GREEN", [CE_BLUE] = "BLUE", }; typedef struct Color Color; struct Color { enum ColorEnum value; }; [[maybe_unused]] static constexpr Color RED = { CE_RED }; [[maybe_unused]] static constexpr Color GREEN = { CE_GREEN }; [[maybe_unused]] static constexpr Color BLUE = { CE_BLUE }; int main() { puts(names[RED.value]); } ``` vs. https://godbolt.org/z/MTWWzzh17 ```c #include <stdio.h> enum ColorEnum { CE_RED, CE_GREEN, CE_BLUE, CE_SIZE }; static char const*const names[CE_SIZE] = { [CE_RED] = "RED", [CE_GREEN] = "GREEN", [CE_BLUE] = "BLUE", }; typedef struct Color Color; struct Color { enum ColorEnum value; }; [[maybe_unused]] static constexpr Color RED = { CE_RED }; [[maybe_unused]] static constexpr Color GREEN = { CE_GREEN }; [[maybe_unused]] static constexpr Color BLUE = { CE_BLUE }; int main() { puts(names[RED.value]); } ``` The unused constexpr names aren't optimized out but the const*const ones are.