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. https://web.archive.org/web/20161217033223/http://b.atch.se/posts/non-constant-constant-expressions/ Non-constant constant-expressions in C++
  2. https://web.archive.org/web/20161217232535/http://b.atch.se/posts/constexpr-counter/ How to implement a constant-expression counter in C++
  3. https://web.archive.org/web/20161217033218/http://b.atch.se/posts/constexpr-meta-container/ How to implement a compile-time meta-container in C++

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