0Pricing
C++ Academy · Lesson

constexpr and Compile-Time

Compute values at compile time.

constexpr and Compile-Time is a free C++ Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What constexpr Means

constexpr tells the compiler a value or function can be evaluated at compile time, producing a true compile-time constant.

#include <iostream>

int main() {
    constexpr int size = 4 * 4;   // computed at compile time
    int arr[size];
    std::cout << sizeof(arr) / sizeof(int) << "\n";
}

constexpr Functions

A constexpr function can run at compile time when given constant arguments, and at runtime otherwise.

#include <iostream>

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

int main() {
    constexpr int s = square(5);   // compile time
    int n = 6;
    std::cout << s << " " << square(n) << "\n";  // second at runtime
}

Compile-Time Constants in Arrays

Because constexpr values are known at compile time, they can size arrays and serve as template arguments.

constexpr vs const

const means "cannot change"; constexpr means "known at compile time". Every constexpr is implicitly const, but not every const is constexpr.

#include <iostream>

int main() {
    int x;
    std::cin >> x;
    const int a = x;        // const but runtime value
    constexpr int b = 10;   // compile-time constant
    std::cout << b << "\n";
}

Recursive constexpr

constexpr functions may recurse, letting the compiler compute things like factorials before the program runs.

#include <iostream>

constexpr long factorial(int n) {
    return n <= 1 ? 1 : n * factorial(n - 1);
}

int main() {
    constexpr long f = factorial(5);  // 120 at compile time
    std::cout << f << "\n";
}

Richer constexpr in Modern C++

Since C++14 constexpr functions can have loops, locals, and branches, letting you compute complex constants at compile time.

#include <iostream>

constexpr int sumTo(int n) {
    int total = 0;
    for (int i = 1; i <= n; ++i) total += i;
    return total;
}

int main() {
    constexpr int s = sumTo(100);
    std::cout << s << "\n";
}

constexpr Variables Must Initialize at Compile Time

A variable declared constexpr must have an initializer evaluable at compile time, or the program will not compile.

Benefits of Compile-Time Computation

Computing at compile time means zero runtime cost, values usable in constant contexts, and earlier error detection.

constexpr and Templates

constexpr values pair naturally with templates and std::array, where sizes and parameters must be compile-time constants.

When It Falls Back to Runtime

If a constexpr function gets non-constant arguments, it simply runs at runtime like a normal function — no error.

Prefer constexpr for Constants

Use constexpr for true constants (sizes, math, lookup tables). It is more expressive than const and enables compile-time guarantees.

Quick Check

Test your understanding of constexpr.

Recap

You learned that constexpr enables compile-time evaluation of variables and functions, differs from const (which only forbids modification), supports loops and recursion in modern C++, and falls back to runtime when given non-constant inputs.

Frequently asked questions

Is the “constexpr and Compile-Time” lesson free?

Yes — the full text of “constexpr and Compile-Time” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

What will I learn in “constexpr and Compile-Time”?

Compute values at compile time. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “constexpr and Compile-Time” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. const Variables and Parameters
  2. const Member Functions
  3. constexpr and Compile-Time
  4. consteval and constinit
← Back to C++ Academy