Caffeinated Coder
Book an appointment
Lesson 23 · Modern C++

const and constexpr

A C++ program lives twice: once while the compiler builds it, and again every time it runs. constexpr asks the compiler to work a value out while it builds, so it is baked into the program and can size an array. const only promises that a value never changes after it is set, and that value may come from run time. Follow a ten-line program as sq(4) is computed by the compiler, argc arrives when the program runs, and two small mistakes stop the build.

12 min11 steps5 questionsBeginner
Fig 23 · Interactive
Loading figure…
or ← →

Quiz Unlocks after the last step.