constexpr vs const in C++: What's the Difference? Complete Guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: The Eternal Battle of Constants in C++

\n\n

If you've ever written in C++ (or even just started learning it), you've certainly encountered const. This magical prefix promises that the value of a variable won't change. But then C++11 came along and brought constexpr with it. And the confusion began.

\n\n

On Stack Overflow, the question "What's the difference between constexpr and const?" has gathered hundreds of thousands of views. Programmers of all levels confuse these two concepts, using them as synonyms. But that's a big mistake!

\n\n

In this tutorial, we'll once and for all figure out how constexpr and const differ in C++. You'll learn:

\n
    \n
  • What a compile-time constant and a runtime constant are.
  • \n
  • Why const isn't always about performance.
  • \n
  • How constexpr turns C++ into a modern and safe language.
  • \n
  • When to use each of these constructs.
  • \n
\n\n

Let's go! 🚀

\n\n

Chapter 1. const: A Promise, Not a Guarantee

\n\n

Let's start with the basics. const in C++ is a type qualifier that tells the compiler: "Hey, this value cannot be changed after initialization." Sounds simple, but there's a catch.

\n\n

Example 1: const in Action

\n\n
#include <iostream>\n\nint main() {\n    const int x = 42;  // Promise: x won't change\n    // x = 100;        // Compilation error — great!\n    \n    std::cout << x;   // Will output 42\n    return 0;\n}
\n\n

Everything looks good. But what if the value of a const variable is only computed at runtime?

\n\n

Example 2: const That Is Computed at Runtime

\n\n
#include <iostream>\n\nint get_value() {\n    return 42;  // Function returns a value\n}\n\nint main() {\n    const int y = get_value();  // Still const?\n    // But the value of y is only known at runtime!\n    \n    // Let's try using it in an array:\n    // int arr[y];  // Error! y is not a compile-time constant!\n    \n    return 0;\n}
\n\n

There it is, the main pitfall! const protects a variable from being modified, but does not guarantee that its value is known at compile time. If the value comes from a function, it's computed only when the program runs.

\n\n

Key takeaway: const is a contract for immutability, not for compile-time computability.

\n\n

Chapter 2. constexpr: Constants That Live in the Code

\n\n

This is where constexpr enters the stage. This specifier appeared in C++11 and fundamentally changed the approach to constants. constexpr means: "This value must be computed at compile time." Or, at the very least, it can be computed.

\n\n

Example 3: constexpr Variable

\n\n
#include <iostream>\n\nint main() {\n    constexpr int z = 42;  // Guarantee: value is known at compile-time\n    \n    int arr[z];  // Works! z is a compile-time constant\n    \n    std::cout << sizeof(arr) / sizeof(arr[0]);  // Will output 42\n    return 0;\n}
\n\n

The difference is obvious: constexpr allows using a variable where the compiler needs to know the exact value — for example, for array sizes or template parameters.

\n\n

Example 4: constexpr Functions

\n\n

But the most interesting part is functions. constexpr can be applied to functions, and then they can be computed at compile time if all arguments are also compile-time constants.

\n\n
#include <iostream>\n\nconstexpr int square(int x) {\n    return x * x;\n}\n\nint main() {\n    constexpr int result = square(5);  // Computed at compile-time!\n    int arr[result];                   // Works\n    \n    std::cout << result;  // 25\n    \n    int runtime_value =                

Blogs

Book Recommendations