OLD VERSION: Understanding The constexpr 2-Step
summary
C++20 makes std::vector and std::string usable in constant expressions yet direct constexpr heap allocation fails because memory allocated at compile time must also be freed at compile time. The practical solution is the constexpr two-step: perform dynamic work at compile time, copy into an oversized array stored as a compile-time template value, then create a right-sized static copy and return a span or string_view. Examples include a 300-angle table (shows 1,696 unused zeros if oversized) and a clang bug around static inside consteval fixed by using an NTTP value.
AI-written summary · about
chapters (10)
- 0:00constexpr two-step concept
- 3:02C++20 containers and memory rules
- 10:58constexpr two-step technique
- 17:02Level one: oversized array
- 27:04Level one review and binary size
- 35:54Level two: string to string_view
- 42:25Level 2.5: span and string_view
- 51:19Right-sizing arrays
- 57:00Level three: vector to span
- 1:00:13Summary and applications
related talks
01h 18m“Variable Templates and Compile-Time Computation with C++14"
01h 04mconstexpr: Introduction
01h 00mconstexpr: Applications
01h 09m“Compile-time programming and reflection in C++20 and beyond”
01h 04m“Constant Fun"
01h 10m“constexpr ALL the Things!”
01h 00mRemoving Metaprogramming From C++, Part 1 of N: constexpr Function Params
03mAnything can be a Constexpr if you try hard enough
