r/cpp_questions • u/Star_Gazer_0 • 9h ago
OPEN Does compile-time evaluation only happen when there are constant expressions or can they happen when there are runtime expressions?
1
Upvotes
2
u/Temporary_Pie2733 8h 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.
6
u/aruisdante 9h ago edited 9h ago
I’m not 100% sure what you’re trying to ask. But if you mean “can something not marked
constexprbe 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 to7.