【发布时间】:2017-11-03 11:58:14
【问题描述】:
我已经阅读了这些材料:
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 的用途之一是将右值引用变量传递给其他人并保证不再使用它。但我不知道为什么不在这里使用move,他说的对吗?
【问题讨论】:
-
答案是:不要。当您想完善前推推导参数(模板/自动)时,使用
std::forward- 这可能等效于std::move(当推导参数是右值引用时)。当你想离开时使用std::move。
标签: c++ c++11 move rvalue-reference rvalue