user20012209
user20012209

Reputation:

Why is not the move constructor called?

As I understand, the next code should lead to a move constructor call, because the ctor argument is a rvalue reference:

#include <iostream>
#include <vector>

class Foo {
public:
    int a, b;

    Foo(int a, int b) : a(a), b(b) {

    }


    Foo(Foo&& other) noexcept : a(other.a), b(other.b) {
        std::cout << "move" << std::endl;
        other = Foo(a, b);
    }

    Foo(const Foo& other) : a(other.a), b(other.b) {
        std::cout << "copy" << std::endl;
    }

    Foo& operator=(const Foo& other) = default;
};

class Wrapper {
public:
    Foo foo;

    explicit Wrapper(Foo&& foo)
            :foo(foo) { }
};

int main()
{
    Foo foo(14, 88);

    Wrapper wrapper(std::move(foo));

    return 0;
}

Nevertheless, there is this console output:

copy

What do I understand wrongly?

Upvotes: 0

Views: 56

Answers (0)

Related Questions