C++ Logo

std-proposals

Advanced search

Re: [std-proposals] this return type

From: Lénárd Szolnoki <cpp_at_[hidden]>
Date: Sun, 09 Apr 2023 16:11:57 +0100
On Sun, 2023-04-09 at 14:09 +0200, Jan Schultke wrote:
> I've looked at the requirements in C++98 and they are similar.
> However, what if S is the base class, and inside of foo(),
> replacement
> of a subobject takes place. We might not be able to transparently
> replace a subobject and have references to the subobject stay valid,
> but what about the base object.
>
> The section you've quoted only limits transparent replacement of
> references to the subobject, but references to the base object might
> need to stay valid.
>

I don't follow, the base object is a subobject.

> In such a case, you could replace the base class subobject (which
> invalidates all references to the subobject), but the base object's
> lifetime should be unaffected, but the vtable would be altered
> because
> of a change in the subobject.
>

What do you mean by "the base object lifetime being unaffected"? I
don't think it's possible to replace the object with a different
dynamic type without destroying and recreating the base subobject as
well, so its lifetime is pretty much affected.

Maybe I misunderstand the argument, a concrete example would help.

Given:

struct S {
  virtual void foo() = 0;
};

void f(S& s) {
  s.foo(); // 1
  s.foo(); // 2
}

Do you have an example invocation of `f` where the dynamic type of `s`
would change after the line marked with `// 1`, and the reference `s`
would also remain valid for the next line to not become undefined
behaviour?

>
> Cheers,
> Jan

Cheers,
Lénárd

Received on 2023-04-09 15:44:02