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

The meaning of "under construction or destruction" is not precise #5177

Open
xmh0511 opened this issue Dec 31, 2021 · 1 comment
Open

The meaning of "under construction or destruction" is not precise #5177

xmh0511 opened this issue Dec 31, 2021 · 1 comment

Comments

@xmh0511
Copy link
Contributor

xmh0511 commented Dec 31, 2021

[class.cdtor] p4 says

Member functions, including virtual functions ([class.virtual]), can be called during construction or destruction ([class.base.init]). When a virtual function is called directly or indirectly from a constructor or from a destructor, including during the construction or destruction of the class's non-static data members, and the object to which the call applies is the object (call it x) under construction or destruction, the function called is the final overrider in the constructor's or destructor's class and not one overriding it in a more-derived class. If the virtual function call uses an explicit class member access ([expr.ref]) 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.

Consider this example

struct B;
struct A{
  A(B*);
  virtual void show(){} // #1
};
struct B: A{
  B():A(this){
  }
  void show(){} // #2
};
A::A(B* ptr){
   ptr->show();
}
int main(){
  B obj; // #3
}

The definition of obj causes the invocation of B::B(), which in turn causes the invocation of A::A(B*). These two objects are both under construction(their constructors are both being called at that point). Obviously, this is not the intent of how to interpret "under construction or destruction" in the original provisions. The construction or destruction of a base class can be within the construction or destruction of a derived class, this causes the issue: which one is referred to by "under construction or destruction"?

I think we should change "the object under construction or destruction" to

the object under nearest construction or destruction; "nearest" means the object's constructor or destructor was most recently entered and not yet exited.

The proposal more conforms to the interpretation exposed by the formal examples. In addition, [class.cdtor] p5 and p6 also have this issue.

@languagelawyer
Copy link
Contributor

Isn't it CWG1517, of which you already aware of?

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