C/C++ Arena

Step 6 of 6

Challenge: compile-time computation

constexpr functions can run during compilation when their inputs are constants. The result is baked into the program as a literal, and static_assert can check it. Lookup tables, hashes of fixed strings, unit conversions and configuration validation are often done this way in performance-critical code.

Since C++20, constexpr functions may use loops, local variables, std::array and even std::vector (as long as it doesn't escape).

Your turn:

Both must work at compile time: the tests use them inside static_assert.

Previous: Constraining templates with concepts