I have read these materials:
What's the difference between std::move and 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?
std::forward
when you want to perfect forward a deduced parameter (template/auto) - this may become equivalent tostd::move
(when the deduced parameter is an rvalue reference). Usestd::move
when you want to move from. – Velarize