Things C++26 define_static_array can't do
13 hours ago
- #constexpr
- #static arrays
- #C++26
- constexpr global variables cannot hold pointers to heap allocations due to the firewall between compile-time evaluation and runtime existence, but contents can be persisted in a global constexpr std::array using the "constexpr two-step" method.
- C++26 introduces std::define_static_array to simplify compile-time array generation, returning a span over a static-storage constant array, but it has limitations compared to the two-step.
- Limitations of define_static_array include: requiring structural types (excluding types like std::optional), inability to handle pointers to string literals, and inability to work with move-only types.
- define_static_array produces immutable arrays in rodata, unlike the two-step which can create mutable arrays, and it uses template parameter objects that are const, preventing mutable static storage.
- Future C++ standards may address these issues through extended NTTP support (e.g., P3380R1 for explicit structural types) or new facilities for static storage manipulation, but define_static_array remains limited in C++26.