inline, constexpr and noexcept Functions
constexpr functions can run at compile time. inline suggests inlining and allows definitions in headers. noexcept promises no exceptions.
Example
#include <iostream>
using namespace std;
constexpr int fact(int n) { return n <= 1 ? 1 : n * fact(n - 1); }
inline int sq(int x) noexcept { return x * x; }
int main() {
constexpr int f5 = fact(5);
static_assert(f5 == 120, "compile time check");
cout << f5 << " " << sq(9) << "\n";
}Result
120 81
Exercise
Compile time function.
int f(int n)Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.