Compile-Time Dynamic Memory Allocation is Real

Keir, P. (Speaker)

Activity: Talk or presentationOral presentation


C++20 extends the capabilities of generalised constant expressions to include transient dynamic memory allocation; which must be freed before evaluation of the surrounding constant expression.

While interactivity, and other ad-hoc restrictions on constant expressions remain, the opportunity now exists to integrate existing and idiomatic stateful programs within type level calculations.

The C++ standard library is the most basic dependency of a project; and shipped with every compiler. Yet, while compile-time dynamic memory allocation is functional, algorithms and containers from the standard library are not available for use within constant expressions.

We introduce the C'est library: providing a growing subset of common functions and classes from the C++ standard library, applicable within constant expressions. The need for strongly-typed allocation arises, and we introduce emergent idioms to overcome the limitations of transient allocation.

Pervasive memory errors, including uninitialised memory access; using non-owned memory; buffer overflows; and double free errors, are all now capable of capture through compilation errors; rather than runtime errors. While template metaprogramming has been avoided, there is nevertheless a development cost, with the capabilities of the debugger stretched; and memory debuggers now indispensable in the quest even to compile larger programs.

We conclude with a case study involving compile-time verification of the Metamath proof database format, and discuss our ongoing work within the Clang constant expression evaluator.
Period21 Oct 2020
Event titleScottish Programming Languages Seminars (Oct 2020 meeting)
Event typeSeminar
Degree of RecognitionNational


  • C++
  • metaprogramming
  • functional programming
  • types
  • templates
  • verification