lundi 30 octobre 2017

Referring member before execution of not trivial constructor

I read c++ 11 draft standard (N3242 revision) and came across on following statement:

(12.7 Construction and destruction). For an object with a non-trivial constructor, referring to any non-static member or base class of the object before the constructor begins execution results in undefined behavior.

As I understand following default constructor of Foo has undefined behaviour (in piece of code i(&a.i)? Constructor of Foo is not trivial (because it is user defined) and I am referring to member a before execution of constructor.

struct A
{
    int i;
};

struct Foo
{
    A a;
    int* i;
    Foo() : a(), i(&a.i)
    {}
};

Aucun commentaire:

Enregistrer un commentaire