Garmaine Staff asked 2 years ago

I define a class A like this:

class A {
public:
    A(){
        cout << "A constructing..." << endl;
    }
    A(const A &a){
        cout << "A copy constructing..." << endl;
    }
    A(A&& a){
        strcpy(name, a.name);
        cout << "A move constructing..." << endl;
    }
    ~A(){
        cout << "A destructing..." << name << endl;
    }
};

And a simple function:

A f(A&& b) {
    cout << "------after call------" << endl;
    A f = b; // Use "A f(b)" to get the same effect
    cout << "------before return------" << endl;
    return f;
}

And when I call auto test = f(move(b));, why would it call copy constructor instead of move constructor? Is b in f() not a rvalue?