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.ref] p1 The evaluation of the id-expression in class member access #5836

Open
xmh0511 opened this issue Sep 15, 2022 · 3 comments
Open

Comments

@xmh0511
Copy link
Contributor

xmh0511 commented Sep 15, 2022

In the c++20 document, [expr.const p5 says

An expression E is a core constant expression unless the evaluation of E, following the rules of the abstract machine ([intro.execution]), would evaluate one of the following:

  • [...]
  • 5.8 an lvalue-to-rvalue conversion unless it is applied to
  • a non-volatile glvalue that refers to an object that is usable in constant expressions, or
  • a non-volatile glvalue of literal type that refers to a non-volatile object whose lifetime began within the evaluation of E;
  • [...]
  • 5.12 an id-expression that refers to a variable or data member of reference type unless the reference has a preceding initialization and either
  • it is usable in constant expressions or
  • its lifetime began within the evaluation of E;

Consider this example:

struct A{
   int const& rf;
};
constexpr int value = 10;
A a{value};
constexpr int v = a.rf;  // error

a.rf does refer to the object value that is usable in constant expressions. The only possible reason why the initialization is not a constant expression should be 5.12. The wording of 5.12 is changed in the current draft, however, which has a similar meaning for this case.

[expr.const] p7

For such a reference that is not usable in constant expressions, the reference is treated as binding to an unspecified object of the referenced type whose lifetime and that of all subobjects includes the entire constant evaluation and whose dynamic type is constexpr-unknown.

So, this issue remains that whether the evaluation of the class member access would evaluate the id-expression. [expr.ref] p1 says

The postfix expression before the dot or arrow is evaluated; the result of that evaluation, together with the id-expression, determines the result of the entire postfix expression.

It is a bit vague whether the id-expression is evaluated. The improvement might be

The postfix expression before the dot or arrow is evaluated; the result of that evaluation, together with the evaluation of the id-expression, determines the result of the entire postfix expression.

The change makes the implied meaning clearer.


Another issue is, whether we admit that the post-expression in the class member access is sequenced before the id-expression

@languagelawyer
Copy link
Contributor

The change makes the implied meaning clearer.

The implied meaning that id-expression is not evaluated

@jensmaurer
Copy link
Member

The id-expression in a class member access is a compile-time thing and "evaluating" it doesn't make much sense; it doesn't have a "value" by itself. Only in conjunction with the object expression does it have a value.

@jensmaurer
Copy link
Member

p5.12 has been removed. p8 has the rule now, and it seems to make clear it that the example is ill-formed.

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

No branches or pull requests

3 participants