r/cpp_questions • • 5d ago

OPEN Does compile-time evaluation only happen when there are constant expressions or can they happen when there are runtime expressions?

1 Upvotes

9 comments sorted by

8

u/aruisdante 5d ago edited 5d ago

I’m not 100% sure what you’re trying to ask. But if you mean “can something not marked constexpr be evaluated at compile time if all of its inputs are known at compile time and the compiler can see the full context needed to compute the result,” then the answer is “yes, but this is a compiler dependent optimization.”

For example with optimizations enabled, both GCC and Clang will compile this program down to simply setting the register for main’s return to 7. 

int add(int const a, int const b) {
    return a+b;
}

int main() {
    return add(5,2);
}

2

u/Star_Gazer_0 5d ago

I meant as like x+2 which imagine in the first scenario is a constant expression because x is a constant variable with an integral type and a constant expression initializer, this will definitely be compile-time evaluated, but if x was not a constant expression, then is it possible that x+2 which overall is not a constant expression, to be compile-time evaluated, or is it strictly runtime evaluated?

8

u/aruisdante 5d ago

Please post an actual code snippet of what you want to understand the difference between, it will help us help you 🙂

4

u/garagedragon 5d ago

You can't evaluate anything at a point where you don't know what concrete value the sub-expressions have, and compile-time happens before run-time. If you only know what value x has at runtime, you don't know at compile-time what the value of x+2 is going to end up being because you don't know the value of x.

2

u/mrtlo 5d ago

I think you can come up with the answer to this yourself. Obviously it has to be able to deduce the result compile time for this to work... To experiment with this you can derive input from command line arguments for a quick way to ensure "impossible to determine" at compile time

1

u/manni66 5d ago

Do you think a compiler can see into the future?

1

u/flatfinger 4d ago

Yes.

unsigned test(unsigned x)
{
    unsigned i=1;
    while((i & 0xFFFF) != x)
        i*=3;
    return x>>16;
}

Both clang and gcc will generate machine code that unconditionally returns zero.

1

u/conundorum 3d ago

x + 2 can, by definition, only be evaluated at compile time if x is known at compile time. The goal of compile-time evaluation is to hard-code pre-calculated values into the executable instead of having to dedicate processor time to calculating them. If it requires any form of runtime evaluation, then it can't be done at compile time.

In this case, the expression x + 2 itself can be encoded at compile time. But doing so would just create callable code... which means that it'll just encode "do this at runtime" into the executable. You might be able to create a lookup table at compile time if x is sufficiently small, so that you can index into it as plusTwo[x], but it's unlikely for the extra space to save enough time to be worth the tradeoff; it would likely be the same or slightly slower.


So, yeah... only compile-time values can get compile-time evaluation. You can evaluate all possible results at compile time if you want, but even then, you'd still need to choose the correct one at runtime.

2

u/Temporary_Pie2733 5d ago

Runtime basically means “involving information only available after execution begins”.  Whether the compiler puts in the effort to correctly identify all expressions that can be evaluated at compile time is another matter.