C++ Logo

std-discussion

Advanced search

Re: Construction and destruction

From: Bo Persson <bo_at_[hidden]>
Date: Mon, 6 Sep 2021 13:32:19 +0200
On 2021-09-06 at 12:50, Vladimir Grigoriev via Std-Discussion wrote:
> I am sorry but I do not see ay relation between the statement I provided
> from the C++ Standard and the following example and your explanation.
> The statement is entirely unclear.
> For example consider the following program
> #include <iostream>
> struct A
> {
> A() = default;
>
> A( const A *a )
> {
> a->f();
> }
>
> virtual void f() const
> {
> std::cout << "Hello!\n";
> }
> };
> int main()
> {
> A a1;
> A a2( &a1 );
>
> return 0;
> }
> Here the object a1 is a complete object. Does it mean that the program
> ill-formed?

No. The call here has nothing to do with the object being constructed,
so those rules do not apply.

Consider a class hierarchy with multiple base classes where virtual
functions are defined in several paths. There you cannot call virtual
functions involving the parts not-yet-constructed.

This example has none of that.


>
> Суббота, 28 августа 2021, 16:51 +03:00 от Jason McKesson via
> Std-Discussion <std-discussion_at_[hidden]>:
> On Sat, Aug 28, 2021 at 6:11 AM Vladimir Grigoriev via Std-Discussion
> <std-discussion_at_[hidden]
> </compose?To=std%2ddiscussion_at_[hidden]>> wrote:
> >
> > This phrase in the paragraph #4 of the section «Construction and
> destruction»
> >
> > «If the virtual function call uses an explicit class member
> access (7.6.1.4) and the object expression refers to the complete
> object of x or one of that object’s base class subobjects but not x
> or one of its base class subobjects, the behavior is undefined.»
> >
> > What is the difference between the complete object of x or one of
> that object’s base class subobjects and the x or one of its base
> class subobjects?
>
> The term "complete object" is defined in [intro.object]. It is an
> object which is not a subobject of another object.
>
> `x` is the object being constructed, and it may be the subobject of
> another object. Thus, the "complete object of x" is the complete
> object in which `x` is a subobject, or just `x` if `x` is not a
> subobject.
>
> Basically, what the text is saying is that if you try to use explicit
> member access to call a virtual function that goes *outside* of the
> inheritance graph of `x`, you get UB.

Received on 2021-09-06 06:32:29