C++ const vs constexpr

The cluster discusses the semantics, compile-time guarantees, optimizations, and differences between `const` and `constexpr` in C++, including debates on their practical uses, error handling, and comparisons to similar features in other languages.

📉 Falling 0.5x Programming Languages
2,943
Comments
19
Years Active
5
Top Authors
#9393
Topic ID

Activity Over Time

2008
1
2009
4
2010
34
2011
36
2012
38
2013
113
2014
62
2015
180
2016
184
2017
201
2018
152
2019
251
2020
225
2021
288
2022
335
2023
340
2024
177
2025
302
2026
22

Keywords

UB JS godbolt.org MyType github.com GCC AIUI BASIC const compile time compile int static tag variables declarations fn variable

Sample Comments

Svoka • Jul 2, 2024 • View on HN

I guess so... This is reason for `consteval` and such.

dangoor • Feb 23, 2016 • View on HN

Wouldn't const be checked at compile time?

Kranar • May 5, 2023 • View on HN

Nothing you've demonstrated requires the use of constexpr. A const is perfectly suited for that.

mwkaufma • Dec 29, 2017 • View on HN

It's like 'const' itself -- it's a tool for producing better error messages, not better final executables.

flohofwoe • Jul 1, 2025 • View on HN

I guess you're not a fan of 'const' either? ;)

keybored • Jul 26, 2024 • View on HN

Does “const” mean “immutable” and “constexpr” mean “compile-time” in C++? That’s funny.

shultays • Feb 16, 2016 • View on HN

static const's may not be optimized to literals. constexpr is not fully supported.

pantalaimon • Nov 14, 2018 • View on HN

Can we please have constexpr in C 2x to put an end to this madness?

Thorrez • Sep 10, 2023 • View on HN

Won't the compiler optimize it away even without constexpr or const? The compiler can see it doesn't get changed.

midjji • Jul 6, 2021 • View on HN

Its better to make it constexpr than const or static.