vendredi 3 novembre 2017

Why use std::forward rather than std::move for data member in rvalue reference constructor's initialization list?

I have read these materials:

What's the difference between std::move and std::forward

std::move Vs std::forward

this answer is very close to my question, but one is setImage, the other is rvalue reference constructor, so I'm afraid something subtle exists.

Forward

class ClassRoom
{
private:
    vector<string> students;

public:
    ClassRoom(vector<string>&& theStudents)
        :students{std::forward<vector<string>>(theStudents)}
    {
    }
}

Move

class ClassRoom
{
private:
    vector<string> students;

public:
    ClassRoom(vector<string>&& theStudents)
        :students{std::move(theStudents)}
    {
    }
}

Someone told me forward is the right method here because one of the usages of forward is to pass rvalue reference variable to others and guarantee not use it again. but I can't figure out why not to use move here and is what he said right?

Aucun commentaire:

Enregistrer un commentaire