jeudi 26 janvier 2017

Understanding which constructor gets called [duplicate]

This question already has an answer here:

I have a struct which stores an int:

struct foo {
    int value;

    foo(int value) : value(value) {
        std::cout << "value constr\n";
    }

    foo(foo const&) {
        std::cout << "copy constr\n";
    }

    foo(foo&&) {
        std::cout << "move constr\n";
    }
};

In the main method I do the following:

foo bar = foo(foo(foo(42)));

At that point I expect the usual constructor to be called at first and then move constructor several times because the argument will be an rvalue. However, the output is only "value constr".

Why neither copy nor move constructors are called and what actually happens in this example?

Aucun commentaire:

Enregistrer un commentaire