Menu

Converting a pointer to a derived class to a pointer to a base class before construction of base

Fraser
2018-01-13
2018-03-15
  • Fraser

    Fraser - 2018-01-13

    I don't think cppcheck can identify the undefined behaviour described in N4713 15.7 section 2. My compiler also does not identify this.

    BTW is section the correct term?

     
  • Daniel Marjamäki

    I believe that Cppcheck does not have such checker. Feel free to create a ticket in Trac. I would like that it contains a minimal example code with UB.

     
  • Fraser

    Fraser - 2018-01-22

    Do I need a different account to log in to Trac? I have made posts before on it.

     
  • Daniel Marjamäki

    yes.. well can you show a minimal example code here and I'll create a ticket.

     
  • Fraser

    Fraser - 2018-01-23

    The following program can check a few things on this part of the standard. It is an extended version of the example in the standard.

    struct A {
    };

    struct B : virtual A {
    };

    struct C : B {
    C(int*);
    };

    struct D : virtual A {
    D(A*);
    int d;
    };

    struct X {
    X(A);
    X(int
    );
    };

    struct E : C, D, X {
    E() : C(&d), // undefined behaviour: the construction of D has not started.
    D(this), // undefined behaviour: upcast from E to A might use path E -> D -> A*
    // but D is not constructed.
    X(this) {} // defined behaviour: upon construction of X, C/B/D/A sublattice is fully constructed
    };

    struct F : C, D, X {
    F() : C(&f), // defined behaviour: F() has started
    D((C)this), // defined behaviour: F -> C is defined because F() has started,
    // and C
    -> A* is defined because C is fully constructed
    X(this) {} // defined behaviour: upon construction of X, C/B/D/A sublattice is fully constructed
    int f;
    };

    int main(int argc, char* argv[])
    {
    return 0;
    }

     
  • Fraser

    Fraser - 2018-03-15

    Has a ticket been made for this?

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.