Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[expr.const] Clarify example on when evaluation takes place #6853

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Eisenwave
Copy link
Contributor

@Eisenwave Eisenwave commented Mar 8, 2024

I believe that [expr.const] Example 8 is at best unclear, and at worst misleading in its current form:

char array[1 + int(1 + 0.2 - 0.1 - 0.1)];   // Must be evaluated during translation
int size = 1 + int(1 + 0.2 - 0.1 - 0.1);    // May be evaluated at runtime

Firstly, I think "must" is pretty strong language for an example. Don't we have style guides against "must" and "may" in examples?

Secondly, the initialization of an array array doesn't take place during translation. The "during translation" clearly refers to the constant-expression in the array size, not to the entire statement.

Thirdly, there is no such thing as "runtime" in C++. In a C++ sense, the second line has to be evaluated during program execution, and I don't believe that the compiler is free to manifestly constant-evaluate the initializer of size as it pleases here. However, evaluation during program execution may actually take place during compilation, according to the "as-if rule". In short:

  • int size = ... is evaluated during program execution
    • with optimizations, this could mean "at compile-time"
    • without optimizations, this could mean "at run-time"

Mixing standardese terms like "during translation" with "at runtime" in two consecutive lines is simply confusing.

It's also worth noting that Note 10 (right above) uses the terminology "during program execution" already; so it makes sense for the example to use this terminology as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant