Koban
Koban

Reputation: 523

Where should I do std::move for a function parameter passed by &&?

For example, if I have a function that moves a vector to a class member:

struct A
{
    void f(std::vector<int> && v)
    {
        m_v = std::move(v); //1
    }

    std::vector<int> m_v;
}

void other_func()
{
    std::vector<int> other_v;
    A a;
    a.f(std::move(other_v)); //2
}

should I use std::move two times? First when I pass the parameter and second when I assign the parameter to the member? At least the code compiles without the first std::move.

Upvotes: 0

Views: 85

Answers (1)

eerorika
eerorika

Reputation: 238381

should I use std::move two times? First when I pass the parameter and second when I assign the parameter to the member?

Yes.

One move is required since the expression other_v is an lvalue. The other move is required since the exporesion v is an lvalue. All id-expressions are lvalues.

Upvotes: 3

Related Questions