我已阅读这些材料:
What's the difference between std::move and std::forward
this answer非常接近我的问题,但一个是setImage
,另一个是右值参考构造函数,所以我担心存在一些微妙的问题。
class ClassRoom
{
private:
vector<string> students;
public:
ClassRoom(vector<string>&& theStudents)
:students{std::forward<vector<string>>(theStudents)}
{
}
}
class ClassRoom
{
private:
vector<string> students;
public:
ClassRoom(vector<string>&& theStudents)
:students{std::move(theStudents)}
{
}
}
有人告诉我forward
是正确的方法,因为forward
的一个用法是将rvalue引用变量传递给其他人,并保证不再使用它。但我无法弄清楚为什么不在这里使用move
并且他说的是对的?
答案 0 :(得分:3)
In this example, both std::move
and std::forward
do the same thing.
This is different if you change the example to a deduced type, e.g.
template<typename Arg>
ClassRoom(Arg&& theStudents)
:students{std::forward<Arg>(theStudents)}
{
}
v.s.
template<typename Arg>
ClassRoom(Arg&& theStudents)
:students{std::move(theStudents)}
{
}
Then:
vector<string> local_students = /* ... */;
ClassRoom cr(local_students)
Arg
deduces to vector<string>&
, which means different things happen
Forward:
forward<vector<string>&>
passes along the lvalue-reference, so the overload of vector::vector
chosen is the copy constructor, local_students
is unaffected
Move:
move<vector<string>&>
casts the lvalue-reference to rvalue-reference, so the overload of vector::vector
chosen is the move constructor, local_students
is now in the moved-from state
答案 1 :(得分:1)
我会在这里使用std::move
。 std::forward
旨在转发普遍引用。在这种情况下没有通用的参考。
虽然在这个例子中它没有区别,但std::move
更简洁,更自我记录。