constexpr int func(int rf){
constexpr int v = rf; // #1
return 0;
}
int main(){
}
Consider the above code, the compiler complains such a code is ill-formed
. The outcome is here:
error: 'rf' is not a constant expression
That is said, the expression at the place that marked with #1 is evaluated by the compiler. I agree that rf
is not a constant expression, because it does violate the following rules:
An expression e is a core constant expression unless the evaluation of e, following the rules of the abstract machine, would evaluate one of the following expressions:
expr.const#2an lvalue-to-rvalue conversion unless it is applied to
- a non-volatile glvalue of integral or enumeration type that refers to a complete non-volatile const object with a preceding initialization, initialized with a constant expression, or
- a non-volatile glvalue that refers to a subobject of a string literal, or
- a non-volatile glvalue that refers to a non-volatile object defined with constexpr, or that refers to a non-mutable subobject of such an object, or
- a non-volatile glvalue of literal type that refers to a non-volatile object whose lifetime began within the evaluation of e;
However, what makes me confused is, I didn't call function func
anywhere in my example, why the compiler evaluate the expression rf
? That makes me don't understand what is evaluation
and what is the execution
.
According to this rule:
intro.execution#18
When calling a function (whether or not the function is inline), every value computation and side effect associated with any argument expression, or with the postfix expression designating the called function, is sequenced before execution of every expression or statement in the body of the called function. For each function invocation F, for every evaluation A that occurs within F and every evaluation B that does not occur within F but is evaluated on the same thread and as part of the same signal handler (if any), either A is sequenced before B or B is sequenced before A.
It sounds like that, only if the corresponding function is called, then the evaluation for expression which is in the function body does occur.
However, Obviously I didn't call func
in my example. So, my questions are:
Question 1:
what situation will the evaluation for expressions occur in?
For constant expressions, there is only a crude tip in the standard, that is, [Note: Constant expressions can be evaluated during translation. — end note]
, there's no more.
Question 2:
As a contrast, If the statement at #1 would be int v = rf;
, Does the compiler evaluate such an expression rf
during translation if I don't call function func
?
Question 3:
what's the difference between evaluation
and execution
?
Question 4:
where's the relevant clause in the standard specified what situation the evaluation for expressions will occur in?