C++ · Module 5 · Lesson 41 min read

inline & constexpr Functions

Function call overhead

A normal call: push arguments, jump, run, return. For a one-line function called in a tight loop, that overhead can matter.

inline

arduino
inline int square(int x) { return x * x; }
  • Historically: a hint to paste the body at the call site
  • Today: compilers decide inlining themselves (with -O2) and mostly ignore the hint
  • The real modern meaning: an inline function may be defined in a header included by many .cpp files without "multiple definition" link errors
  • Functions defined inside a class body are implicitly inline

constexpr functions

arduino
constexpr int factorial(int n) {
    return n <= 1 ? 1 : n * factorial(n - 1);
}
constexpr int f5 = factorial(5);   // computed at compile time
int x = factorial(user_input);     // also works at runtime
  • Can run at compile time if the arguments are constants
  • Implicitly inline
  • Since C++14 they can contain loops, local variables and if statements
  • Since C++20 they can even use std::vector and std::string (temporarily)

consteval (C++20) — immediate functions

Must be evaluated at compile time; calling with a runtime value is an error.

if consteval (C++23) / std::is_constant_evaluated() (C++20)

Lets a function behave differently at compile time vs runtime.

Why compute at compile time?

  • Zero runtime cost (lookup tables, unit conversions, hashes)
  • Errors found while compiling
  • Use with static_assert to test functions without running the program

Macro functions — avoid

scss
#define SQUARE(x) x * x
SQUARE(2 + 3)   // 2 + 3 * 2 + 3 = 11, not 25!

constexpr functions are type-safe and evaluate arguments once.

Example

C++
#include <array>
#include <iostream>

#define SQUARE_MACRO(x) x * x                   // the classic macro bug

inline int square(int x) { return x * x; }

constexpr long long factorial(int n) {
    long long result = 1;
    for (int i = 2; i <= n; ++i) result *= i;   // loops allowed since C++14
    return result;
}

constexpr bool is_prime(int n) {
    if (n < 2) return false;
    for (int d = 2; d * d <= n; ++d)
        if (n % d == 0) return false;
    return true;
}

// A lookup table built entirely at compile time
constexpr std::array<int, 10> make_squares() {
    std::array<int, 10> t{};
    for (int i = 0; i < 10; ++i) t[i] = i * i;
    return t;
}
constexpr auto SQUARES = make_squares();

// Compile-time unit tests: the program won't compile if these are wrong
static_assert(factorial(5) == 120);
static_assert(is_prime(97) && !is_prime(91));
static_assert(SQUARES[9] == 81);

int main() {
    std::cout << "SQUARE_MACRO(2 + 3) = " << SQUARE_MACRO(2 + 3) << " (wrong!)\n";
    std::cout << "square(2 + 3)       = " << square(2 + 3) << '\n';

    constexpr long long f20 = factorial(20);       // computed by the compiler
    std::cout << "20! = " << f20 << '\n';

    int n = 13;                                    // runtime value: same function, runs at runtime
    std::cout << n << "! = " << factorial(n) << '\n';

    std::cout << "Primes below 30:";
    for (int i = 0; i < 30; ++i)
        if (is_prime(i)) std::cout << ' ' << i;
    std::cout << "\nSquares table:";
    for (int s : SQUARES) std::cout << ' ' << s;
    std::cout << '\n';
    return 0;
}

/* Output:
SQUARE_MACRO(2 + 3) = 11 (wrong!)
square(2 + 3)       = 25
20! = 2432902008176640000
13! = 6227020800
Primes below 30: 2 3 5 7 11 13 17 19 23 29
Squares table: 0 1 4 9 16 25 36 49 64 81
*/