dimanche 10 septembre 2017

reinit variable with default constructor plus assigment

Consider such code:

struct Foo {
    int a;
    Foo() { a = 17; }
    //void clear() { a = 17; }
};

class Boo {
public:
    void f() {
        foo = Foo();
        //foo.clear();
    }
private:
    Foo foo;
};

I compiled it with gcc (-std=c++11), and according to logs in f function foo was not reintialized with foo = Foo(); expression, but all works fine if I uncomment code with clear.

Is code like foo = Foo(); should be translated to

Foo tmp;
memcpy(&foo, &tmp, sizeof(Foo));

Or compiler can do some kind of optimization, and after foo = Foo(); I got garbage in foo?

Aucun commentaire:

Enregistrer un commentaire