Things C++26 define_static_array can't do

· coding · Source ↗

TLDR

  • C++26 define_static_array cleanly materializes constexpr-time data into static storage, but has four structural gaps the older “constexpr two-step” handles fine.

Key Takeaways

  • define_static_array returns a span<const T> over a rodata array; it cannot produce mutable static storage, unlike the two-step lambda approach.
  • It requires every element to be a structural NTTP-compatible type, blocking optional<int>, string, string_view, and span outright.
  • Pointers to string literals fail because they are not valid template arguments, even though const char* itself is a structural type.
  • Move-only types are blocked: creating a template parameter object requires copying the element, so types without copy constructors cannot be used.
  • Barry Revzin’s P3380R1 would let programmers mark types as explicitly structural, potentially fixing gaps 1-3, but at the cost of arcane annotations.

Hacker News Comment Review

  • The single substantive comment frames the limitations as emblematic of C++ complexity accrual: solving self-created problems yields elaborate machinery with conspicuous gaps.

Notable Comments

  • @oseityphelysiol: “C++ people are great at making problems for themselves and then solving them to no end.”

Original | Discuss on HN