Ch7: Compile‑Time Programming

Overview

Compile‑time programming allows the compiler to evaluate expressions, functions, and objects before the program runs. This can improve safety, correctness, and performance by catching errors early and avoiding unnecessary runtime work.

In this chapter, you will learn the core tools of compile‑time programming in C++:

These features let you write code that can run either at compile time or runtime depending on context, and in some cases must run at compile time.

Contents

What you should know before starting

This chapter assumes you already understand:

No templates, classes, or advanced metaprogramming are required. All examples use simple, practical C++.

Why compile‑time programming matters

Modern C++ encourages using compile‑time features when they make code clearer and safer, not for clever tricks.