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

[basic.lval] p5 value vs initialization #4854

Open
xmh0511 opened this issue Sep 1, 2021 · 2 comments
Open

[basic.lval] p5 value vs initialization #4854

xmh0511 opened this issue Sep 1, 2021 · 2 comments

Comments

@xmh0511
Copy link
Contributor

xmh0511 commented Sep 1, 2021

The result of a prvalue is the value that the expression stores into its context; a prvalue that has type cv void has no result. A prvalue whose result is the value V is sometimes said to have or name the value V.

In my mind, the value mentioned in the above rule is used to computes the value of an operand of an operator for the purpose. However, a prvalue is not only for this purpose but also perform initialization. What I want to say is it also store the initialization into its context, which is hinted in [conv.general] p3 and [conv.general] p6

An expression E can be implicitly converted to a type T if and only if the declaration T t=E; is well-formed, for some invented temporary variable t ([dcl.init]).

The effect of any implicit conversion is the same as performing the corresponding declaration and initialization and then using the temporary variable as the result of the conversion. The result is an lvalue if T is an lvalue reference type or an rvalue reference to function type ([dcl.ref]), an xvalue if T is an rvalue reference to object type, and a prvalue otherwise. The expression E is used as a glvalue if and only if the initialization uses it as a glvalue.

Consider an lvalue-to-rvalue conversion applies to a glvalue of class type, the result of that conversion is defined as

Otherwise, if T has a class type, the conversion copy-initializes the result object from the glvalue.

As per [conv.general] p3 and [conv.general] p6, this conversion as if performs that

T t = glvalue;

In this declaration, the copy-initialization is performed like that call a constructor with the argument glvalue to initialize the object. In this implicit conversion, the t is the result of the lvalue-to-rvalue conversion and it is prvalue, hence the modeled initialization will not actually perform except that there exists a result object. I think the value in the definition of the result of a prvalue should have a more general meaning.

The result of a prvalue is the value or initialization that the expression stores into its context; a prvalue that has type cv void has no result. A prvalue whose result is the value V is sometimes said to have or name the value V.

This modification may make the meaning of the prvalue in the new expression value category system be clearer. In the current, the concept of prvalue especially for class type is intricacy.

@jensmaurer
Copy link
Member

"The initialization that the expression stores into its context" does not make sense.

For all uses of prvalue (except some unevaluated contexts), we ensure a result object is provided (possibly via temporary materialization), so there will always be a value / an object to talk about. What the expression stores might be described by way of initialization ("as-if"), but it's still an expression. Example: A(B(0)) for class types A and B ends up as a C-style explicit cast, but it's still an expression, and all semantic constraints to be able to actually initialize the result object (associated with the resulting prvalue) have been checked, although the specific result object might be unknown at that point. (Consider the prvalue result object as a hidden reference parameter.)

I'm willing to consider improvements in the wording here, but sprinkling "initialization" across the normative wording is not helpful.

@xmh0511
Copy link
Contributor Author

xmh0511 commented Sep 2, 2021

What the expression stores might be described by way of initialization ("as-if")

Yes, I just meant this meaning. The prvalue records the recipe about how to initialize an object, which is emulated by the model declaration(T t = E;) or is what we specified it in certain rules(e.g. [expr.static.cast] p4 "Otherwise, the result object is direct-initialized from E.", moreover, we can also use T t = E; to model it). Whenever a result object is supplied for this prvalue, it will impose the recorded recipe to the result object to perform the actual initialization. This is informally spoken, I have not yet found out a formal way to phrase it.

but it's still an expression

IIUC, a prvalue is not always an expression of the grammar. The prvalue also can be produced in a full-expression of the initialization.

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

2 participants