Title
Unclear/missing description of behavior during construction/destruction
Status
drafting
Section
11.9.5 [class.cdtor]
Submitter
Daniel Krügler

Created on 2012-07-07.00:00:00 last changed 123 months ago

Messages

Date: 2013-08-15.00:00:00

Proposed resolution (August, 2013):

  1. Change 11.9.5 [class.cdtor] paragraph 1 as follows:

  2. For an object with a non-trivial constructor, referring to any non-static member or virtual base class of the object before the constructor begins execution results in undefined behavior. For an object with a non-trivial destructor, referring to any non-static member or virtual base class of the object after the destructor finishes execution results in undefined behavior. [Example:
      struct X { int i; };
      struct Y : X { Y(); };                       // non-trivial
      struct A { int a; };
      struct B : public virtual A { int j; Y y; }; // non-trivial
    
      extern B bobj;
      B* pb = &bobj;                               // OK
      int* p1 = &bobj.a;                           // undefined, refers to base class member
      int* p2 = &bobj.y.i;                         // undefined, refers to member's member
    
      A* pa = &bobj;                               // undefined, upcast to a virtual base class type
      B bobj;                                      // definition of bobj
    
      extern X xobj;
      int* p3 = &xobj.i;                           //OK, X is a trivial class
      X xobj;
    
  3. Change 11.9.5 [class.cdtor] paragraphs 3-6 as follows:

  4. To explicitly or implicitly convert a pointer (a glvalue) referring to an object of class X to a pointer (reference) to a direct or indirect virtual base class B of X, the construction of X and the construction of all of its direct or indirect bases that directly or indirectly derive from for which B is a direct or indirect virtual base shall have started and the destruction of these classes shall not have completed, otherwise the conversion results in undefined behavior. To form a pointer to (or access the value of) a direct non-static member...

    Member functions, including virtual functions (11.7.3 [class.virtual]), can be called during construction or destruction (11.9.3 [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 (7.6.1.5 [expr.ref]) and the object expression refers to the complete object of x or one of that object's base class subobjects but not to x or one of its base class subobjects, the behavior is undefined. The period of construction of an object or subobject whose type is a class type C begins immediately after the construction of all its base class subobjects is complete and concludes when the last constructor of class C exits. The period of destruction of an object or subobject whose type is a class type C begins when the destructor for C begins execution and concludes immediately before beginning the destruction of its base class subobjects. A polymorphic operation is a virtual function call (7.6.1.3 [expr.call]), the typeid operator (7.6.1.8 [expr.typeid]) when applied to a glvalue of polymorphic type, or the dynamic_cast operator (7.6.1.7 [expr.dynamic.cast]) when applied to a pointer to or glvalue of a polymorphic type. A polymorphic operand is the object expression in a virtual function call or the operand of a polymorphic typeid or dynamic_cast.

    During the period of construction or period of destruction of an object or subobject whose type is a class type C (call it x), the effect of performing a polymorphic operation in which the polymorphic operand designates x or a base class subobject thereof is as if the dynamic type of the object were class C. [Footnote: This is true even if C is an abstract class, which cannot be the type of a most-derived object. —end footnote] If a polymorphic operand refers to an object or subobject having class type C before its period of construction begins or after its period of destruction is complete, the behavior is undefined. [Note: This includes the evaluation of an expression appearing in a mem-initializer of C in which the mem-initializer-id designates C or one of its base classes. —end note] [Example:

      struct V {
        V();
        V(int);
        virtual void f();
        virtual void g();
      };
    
      struct A : virtual V {
        virtual void f();
        virtual int h();
        A() : V(h()) { }     // undefined behavior: virtual function h called
                             // before A's period of construction begins
      };
    
      struct B : virtual V {
        virtual void g();
        B(V*, A*);
      };
    
      struct D : A, B {
        virtual void f();
        virtual void g();
        D() : B((A*)this, this) { }
      };
    
      B::B(V* v, A* a) {
        f();                 // calls V::f, not A::f
        g();                 // calls B::g, not D::g
        v->g();              // v is base of B, the call is well-defined, calls B::g
        a->f();              // undefined behavior, a's type not a base of B
        typeid(*this);       // type_info for B
        typeid(*v);          // well-defined: *v has type V, a base of B,
                             // so its period of construction is complete;
                             // yields type_info for B
        typeid(*a);          // undefined behavior: A is not a base of B,
                             // so its period of construction has not begun
        dynamic_cast<B*>(v); // well-defined: v has type V*, V is a base of B,
                             // so its period of construction is complete;
                             // results in this
        dynamic_cast<B*>(a); // undefined behavior: A is not a base of B,
                             // so its period of construction has not begun
      }
    

    end example]

    The typeid operator (7.6.1.8 [expr.typeid]) can be used during construction or destruction (11.9.3 [class.base.init]). When typeid is used in a constructor (including the mem-initializer or brace-or-equal-initializer for a non-static data member) or in a destructor, or used in a function called (directly or indirectly) from a constructor or destructor, if the operand of typeid refers to the object under construction or destruction, typeid yields the std::type_info object representing the constructor or destructor's class. If the operand of typeid refers to the object under construction or destruction and the static type of the operand is neither the constructor or destructor's class nor one of its bases, the result of typeid is undefined.

    dynamic_casts (7.6.1.7 [expr.dynamic.cast]) can be used during construction or destruction (11.9.3 [class.base.init]). When a dynamic_cast is used in a constructor (including the mem-initializer or brace-or-equal-initializer for a non-static data member) or in a destructor, or used in a function called (directly or indirectly) from a constructor or destructor, if the operand of the dynamic_cast refers to the object under construction or destruction, this object is considered to be a most derived object that has the type of the constructor or destructor's class. If the operand of the dynamic_cast refers to the object under construction or destruction and the static type of the operand is not a pointer to or object of the constructor or destructor's own class or one of its bases, the dynamic_cast results in undefined behavior. [Example:

      struct V {
        virtual void f();
      };
    
      struct A : virtual V { };
    
      struct B : virtual V {
        B(V*, A*);
      };
    
      struct D : A, B {
        D() : B((A*)this, this) { }
      };
    
      B::B(V* v, A* a) {
        typeid(*this);       // type_info for B
        typeid(*v);          // well-defined: *v has type V, a base of B
                             // yields type_info for B
        typeid(*a);          // undefined behavior: type A not a base of B
        dynamic_cast<B*>(v); // well-defined: v of type V*, V base of B
                             // results in B*
        dynamic_cast<B*>(a); // undefined behavior,
                             // a has type A*, A not a base of B
    

    end example]

Date: 2015-05-25.00:00:00

The current wording of 11.9.5 [class.cdtor] paragraph 4 does not describe the behavior of calling a virtual function in a mem-initializer for a base class, only for a non-static data member. Also, the changes for issue 1202 should have been, but were not, applied to the description of the behavior of typeid and dynamic_cast in paragraphs 5 and 6.

In addition, the resolution of issue 597 allowing the out-of-lifetime conversion of pointers/lvalues to non-virtual base classes, should have been, but were not, applied to paragraph 3.

(See also issue 2056.)

History
Date User Action Args
2014-03-03 00:00:00adminsetstatus: review -> drafting
2013-09-03 00:00:00adminsetmessages: + msg4470
2013-09-03 00:00:00adminsetstatus: drafting -> review
2012-07-07 00:00:00admincreate