cplusplus / cplusplus/draft

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

Open
#5,177 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

cwg not-editorial
Dominant language
TeX
Stars
221
Forks
813
Avg merge
16h 4m
Merged PRs (30d)
36

Description

[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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with [class.cdtor] paragraphs 4–6 and the supplied construction example. Check how the proposed “nearest” wording distinguishes nested construction and destruction, then update the relevant standard wording if accepted and verify that the cited examples remain consistent.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
documentation
Issue type
Documentation
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.