The lifetime of an object o of type T ends when:
if T is a class type with a non-trivial destructor ([class.dtor]), the destructor call starts, or the storage which the object occupies is released, or is reused by an object that is not nested within o ([intro.object]).
class B{public: virtual void f(){ }};
class D: public B{public: virtual void f(){ }};
D d;B* pb = &d;
new (pb) B;
The first paragraph of [basic.life] says that (emphasis mine):The lifetime of an object o of type T ends when:
if T is a class type with a non-trivial destructor ([class.dtor]), the destructor call starts, or the storage which the object occupies is released, or is reused by an object that is not nested within o ([intro.object]).But let us consider the following situation:
class B{public:virtual void f(){ }};class D: public B{public:virtual void f(){ }};D d;B* pb = &d;
new (pb) B;
In [intro.object] it is said that a base class subobject is a subobject, and a subobject is considered to be nested.Therefore the B class subobject is nested within the object "d", therefore the lifetime of "d" should not end.
class A{};
class X{
unsigned char mem[sizeof(A)];
};
X x;
new(x.mem) A;
Thank you for your response.So how can then an object that is nested within "o" reuse the storage of "o"?
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussion+unsubscribe@isocpp.org.
To post to this group, send email to std-dis...@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-discussion/.
class X{};
X x;
new (&x) X;
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussio...@isocpp.org.
Ok, now I understand. So if a new object replaces some subobject of o under the conditions stated in [intro.object] p2, the new object becomes a subobject of o. Therefore it is nested within o. As such, the lifetime of o does not end, because its storage was reused by a new object that is nested within o. Thank you.However, I have a related question. Does the standard say anything about the following situation?
class X{};
X x;
new (&x) X;
Because [basic.life] p8 only covers cases when the lifetime of the old object already ended. I know that x's lifetime will end when its storage is reused by the new object, however this happens "at the same time" as the creation of the new object, not before, therefore I do not know whether [basic.life] p8 still applies.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussion+unsubscribe@isocpp.org.
Yes, if it does not affect anything else in the standard, I believe the modification that you propose would clarify this situation.Hopefully just one last question, what exactly are you referring to by "(Although forming the access path itself may impose additional requirements.)"? Is it related to how you must use "std::launder" in special cases?
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussion+unsubscribe@isocpp.org.
[...] the glvalue is used to access the object [...]
int* p = &(arr[1]);
new (&arr) A;
*p = 0;
I apologize for insisting, I want to make sure I understand these rules. Were my previous assumptions correct? Thank you.
On Sunday, February 11, 2018 at 12:02:27 PM UTC+2, razvyb...@gmail.com wrote:The example that you showed is UB because it violates [basic.life] p7.1, correct?[...] the glvalue is used to access the object [...]However, p8 assures us that the following is legal (given that indeed the creation of A causes a new "int" object to be created in place of arr[1]), am I right?
int* p = &(arr[1]);
new (&arr) A;
*p = 0;
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussion+unsubscribe@isocpp.org.
Perhaps we could clarify this by changing the "The lifetime of an object o of type T ends when" in p1 to say something like "[...] ends immediately before".