Mutable compile-time state in C++

From Esolang
Jump to navigation Jump to search

In C++, constexpr functions are supposed to be pure functions in the Haskell sense: deterministic (the return value is a mathematical function of the arguments) and without side effects. But the rules of C++ got so complicated that there turned out to be ways to create impure constexpr functions.

The methods use SFINAE to detect whether some function is defined. Most of the straightforward ways of these are undefined behavior without diagnostics according to the C++ standard. But Filip Roséen found techniques that are genuinely allowed by the C++ standard, and not just because of some trivial to fix mistakes. He described these in a series of three blog posts in 2015.

  1. Non-constant constant-expressions in C++
  2. How to implement a constant-expression counter in C++
  3. How to implement a compile-time meta-container in C++

(Also known as stateful meta-programming in C++)