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

Conflict for the result object of a prvalue #4885

Closed
geryogam opened this issue Sep 9, 2021 · 13 comments
Closed

Conflict for the result object of a prvalue #4885

geryogam opened this issue Sep 9, 2021 · 13 comments

Comments

@geryogam
Copy link
Contributor

geryogam commented Sep 9, 2021

int i;
i = 1;

Does the right operand 1 (prvalue) of the built-in assignment operator = in i = 1; have a result object or not?

  • No according to [basic.lval/5] (bold emphasis mine):

    The result object of a prvalue is the object initialized by the prvalue; a non-discarded prvalue that is used to compute the value of an operand of a built-in operator or a prvalue that has type cv void has no result object.

  • Yes according to [basic.life/8] (bold emphasis mine) since a new object is initialized at the original storage location:

    If, after the lifetime of an object has ended and before the storage which the object occupied is reused or released, a new object is created at the storage location which the original object occupied, a pointer that pointed to the original object, a reference that referred to the original object, or the name of the original object will automatically refer to the new object and, once the lifetime of the new object has started, can be used to manipulate the new object, if the original object is transparently replaceable (see below) by the new object.

@jensmaurer
Copy link
Member

The built-in assignment operator does not create a new object. Plus, the [basic.life] p8 quote does not talk about "result object" of a prvalue at all, it just talks about objects in general. I'm not seeing a conflict.

@geryogam
Copy link
Contributor Author

geryogam commented Sep 9, 2021

The built-in assignment operator does not create a new object.

Yet [basic.life/8] specifies that object replacement creates a new object: ‘a new object is created at the storage location which the original object occupied’.

Plus, the [basic.life] p8 quote does not talk about "result object" of a prvalue at all

Yet [basic.life/8] specifies that object replacement creates (i.e. initializes) a new object, and [basic.lval/5] defines a result object as a new object that is created (i.e. initialized): ‘The result object of a prvalue is the object initialized by the prvalue;’

@jensmaurer
Copy link
Member

No.

[intro.object] p1 lists the situations when objects are created, and assignment is not in the list. So, assignment does not create an object (it just replaces the value of an existing object).

@geryogam
Copy link
Contributor Author

geryogam commented Sep 9, 2021

So, assignment does not create an object (it just replaces the value of an existing object).

In that case this sentence in [basic.life/8] is incorrect:

If, after the lifetime of an object has ended and before the storage which the object occupied is reused or released, a new object is created at the storage location which the original object occupied, a pointer that pointed to the original object, a reference that referred to the original object, or the name of the original object will automatically refer to the new object and, once the lifetime of the new object has started, can be used to manipulate the new object, if the original object is transparently replaceable (see below) by the new object.

@JohelEGP
Copy link
Contributor

JohelEGP commented Sep 9, 2021

It is not incorrect. It is orthogonal to your issue.

@geryogam
Copy link
Contributor Author

geryogam commented Sep 9, 2021

Could you elaborate?

@JohelEGP
Copy link
Contributor

JohelEGP commented Sep 9, 2021

The precondition in the sentence does not apply.

@geryogam
Copy link
Contributor Author

geryogam commented Sep 9, 2021

Which sentence, which precondition?

@JohelEGP
Copy link
Contributor

JohelEGP commented Sep 9, 2021

If, after the lifetime of an object has ended and before the storage which the object occupied is reused or released, a new object is created at the storage location which the original object occupied, a pointer that pointed to the original object, a reference that referred to the original object, or the name of the original object will automatically refer to the new object and, once the lifetime of the new object has started, can be used to manipulate the new object, if the original object is transparently replaceable (see below) by the new object.

The first part of the precondition does not occur in your example.

int i;
i = 1;

Feel free to join #standardese at the C++ Slack. There, we can discuss this and whether something might be editorial.

@jensmaurer
Copy link
Member

@maggyero, the sentence reads "If [...] a new object is created..."
I have shown that no new object is created for the assignment, so the "if" is not satisfied, and the sentence in [basic.life] simply doesn't apply. "If I ate a banana, I would be full." If I haven't eaten a banana, this is not a statement whether I'm full or not (I might be full for other reasons, or not).

@geryogam
Copy link
Contributor Author

[intro.object] p1 lists the situations when objects are created, and assignment is not in the list. So, assignment does not create an object (it just replaces the value of an existing object).

Do you mean that when built-in assignment replaces the value of an existing object, the lifetime of the existing object continues with the new value without interruption, that is without destruction of the existing object and creation of a new object in the same storage location?

@jwakely
Copy link
Member

jwakely commented Sep 10, 2021

Yes

@geryogam
Copy link
Contributor Author

Okay so my mental model of objects was wrong. An object is not a stored value as I thought, since its value can change with built-in assignment as you explained. But nor is an object a storage location, since the storage location of an object can be reused deterministically with placement new by another object before the storage location is released.

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

4 participants