C/C++ Arena

Step 4 of 8

constexpr

constexpr functions can run at compile time when given constant inputs. The result is baked into the program:

constexpr int square(int x) { return x * x; }
static_assert(square(12) == 144);   // checked by the compiler!
int arr[square(3)];                 // array size known at compile time

Your turn: write constexpr long long power(long long base, int exp). The tests include static_asserts, so if your function can't run at compile time, it won't even compile.

Previous: Class templates Next: if constexpr