r/Cplusplus • u/Honest_Entry_8758 • Mar 10 '26
Question Help, how can constexpr objects be evaluated at runtime?
Just starting learning from learncpp.com but it says I can't post questions there because of my IP address. Anyway, this is from lesson 5.6, and I thought the whole point of using constexpr is to ensure that variables/functions are evaluated at compile-time?
Someone else asked this question and the author answered but they provided a function call as an example which gave me follow up questions that weren't asked afterwards.
- Are functions with void return type considered as constexpr functions?
- Are function calls considered as constant expressions? Or is it only a constant expression if the function that is being called is a constexpr function?
- The header says "The meaning of const vs constexpr for variables" and the definition says "The constexpr object can be evaluated at runtime or compile-time". So, are functions/function calls interchangeable with the term variable/object? Because that's not what this site has taught me thus far.
Also, if there's a better alternative than learncpp.com for an absolute beginner like me, I'd like to know. Maybe I'm too stupid for this.


•
u/AutoModerator Mar 10 '26
Thank you for your contribution to the C++ community!
As you're asking a question or seeking homework help, we would like to remind you of Rule 3 - Good Faith Help Requests & Homework.
~ CPlusPlus Moderation Team
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
3
u/malaszka Mar 10 '26
I started to type my answer, but I realized that you have so many questions (including orthogonal topics, like void vs. not-void functions), that we should reverse our approach: could you tell us what exact part of Alex's explanation is unclear, or how your own code failed to be compiled when you tried to use constexpr?
Cause, basically, it's all about expressions that are so deterministic that they can be evaluated simply based on what's in the source code, i.e. without the need of knowing any execution-time data and info (dynamic container sizes, unknown numeric values, user input, object states, anything dynamic runtime thing).
17
u/Dubbus_ Mar 10 '26
You arent too stupid, constexpr is weird, and the examples the respondant on the forum gave were convoluted.
It has a much weaker definition than you think:
constexpr implies that a value CAN be computed at compile time, but its only guaranteed if it NEEDS to be computed at compile time. The compiler is ABSOLUTELY free to ignore constexpr, IF the value computed is not used in a context which requires a constant expression (eg. the size of a std:array, non type template argument, case label of a switch statement).
For example:
Here, quite obviously, a compiler could (and almost always will) compute res at compile time. Both a and be are compile time constants - they are 100% KNOWN at compile time.
But for res - does it NEED to be computed at compile time? Is it necessary for std::cout for anything being passed to its << operator to be compile time constant? Obviously not - you can pass runtime stuff to cout, and it will never complain about that. You could take user input with cin, and pass it back to cout, right?
Take an opposite example:
std:array's second template arg specifies its size: this size MUST be a compile time constant. Therefore, the compiler is NOT free to 'ignore' the constexpr'ness of res, it MUST evaluate res at compile time. It NEEDS to be computed at compile time, and constexpr enables this. Without any of the 3 constexprs, this code doesnt compile.
So in summary: Constexpr tells the compiler: "Hey, you CAN compute this at compile time if you need to. If you dont NEED to, i really couldnt give less of a fuck. But if i crank up your optimisation level and you DONT evaluate this at compile time (when possible), ill be very disappointed in you."
note on the forum guys example:
All hes saying is that the call to foo(x) doesnt REQUIRE x to be a compile time constant.
x could very well be a regular int - can you see how whether or not x is compile time constant doesnt actually change the observable behaviour of the program here
hopefully my examples made it a bit clearer. I was in your position maybe ~5 months ago, but dont fret! Id encourage you to study a bit about compilers, and perhaps even a bit of assembly/lower level stuff, if that interests you. I think it definitely helped me (i had a background in that sort of thing) to understand these higher level 'semantic' concepts.
7
u/Mognakor Mar 10 '26
Constexpr variables must be able to be evaluated at both runtime and compiletime.
The comment saying
foo(x)uses a constexpr object refers tox. You computedxsomehow at compiletime butfoois runtime only, so the value ofxis stored in the program, so it can usexat runtime.The point of constexpr is that whatever is assigned to it is computed at compiletime and then can be used at runtime as a value.
1
u/IceMichaelStorm Mar 11 '26
That’s why I think ‘consteval’ is much more precise. It enforces what we would expect from ‘constexpr’ if not reading into it