std::move
和std::forward
之间的区别,最好附带一些代码示例?- 如何轻松地理解它们,以及何时使用哪个?
std::move
和std::forward
之间的区别,最好附带一些代码示例?std::move
接受一个对象,并允许您将其视为临时对象(rvalue)。虽然这不是语义要求,但通常接受 rvalue 引用的函数会使其无效。当您看到 std::move
时,它表示对象的值在之后不应再被使用,但您仍然可以分配一个新值并继续使用它。
std::forward
只有一个用例:将模板化函数参数(在函数内部)转换为调用者用于传递它的值类别(lvalue 或 rvalue)。这允许将 rvalue 参数作为 rvalue 传递,并将 lvalue 作为 lvalue 传递,这种方案称为“完美转发”。
来 说明:
void overloaded( int const &arg ) { std::cout << "by lvalue\n"; }
void overloaded( int && arg ) { std::cout << "by rvalue\n"; }
template< typename t >
/* "t &&" with "t" being template param is special, and adjusts "t" to be
(for example) "int &" or non-ref "int" so std::forward knows what to do. */
void forwarding( t && arg ) {
std::cout << "via std::forward: ";
overloaded( std::forward< t >( arg ) );
std::cout << "via std::move: ";
overloaded( std::move( arg ) ); // conceptually this would invalidate arg
std::cout << "by simple passing: ";
overloaded( arg );
}
int main() {
std::cout << "initial caller passes rvalue:\n";
forwarding( 5 );
std::cout << "initial caller passes lvalue:\n";
int x = 5;
forwarding( x );
}
static_cast
并编写一个好的解释来说明您正在做什么。std::forward
的唯一用例是完美转发函数参数,这可能不准确。我遇到过其他情况需要完美转发,例如对象成员。 - Geoff Romerstd::forward
和std::move
都只是类型转换。
X x;
std::move(x);
上面的代码将类型为X的左值表达式x
转换为类型为X的右值表达式(确切地说是xvalue) 。move
还可以接受一个右值:
std::move(make_X());
在这种情况下,它是一个身份函数:接受类型为X的右值并返回类型为X的右值。
使用std::forward
可以在一定程度上选择目标:
X x;
std::forward<Y>(x);
将类型为 X 的左值表达式 x 转换为类型为 Y 的表达式。对 Y 可以使用的约束有限。std::forward
完美地将一个对象转发到函数中,那么在该函数执行后,我可以使用该对象吗?我知道,在std::move
的情况下,这是未定义的行为。 - iammilindmove
: https://dev59.com/rGw05IYBdhLWcg3wykzn#7028318。对于`forward`,如果传递一个左值引用,你的API应该像接收到左值引用一样做出反应。通常这意味着该值将不被修改。但是如果它是一个非const左值引用,则API可能已经对其进行了修改。如果传入一个右值引用,这通常意味着你的API可能已经从中移动,因此https://dev59.com/rGw05IYBdhLWcg3wykzn#7028318 适用。 - Howard Hinnant我认为比较两个示例实现可以深入了解它们的用途和区别。
让我们从std::move
开始。
std::move
简而言之:std::move
用于将任何东西转换为右值(¹),以使其看起来像一个临时对象(即使它不是:std::move(non_temporary)
),以便可以从中窃取其资源,即从中移动(前提是没有被const
属性阻止;是的,右值可以是const
的,这种情况下无法从中窃取资源)。
std::move(x)
表示嗨,伙计们,注意一下,我将把这个x
交给的人可以随意使用和拆分它,因此通常将其用于右值引用参数,因为您确定它们绑定到临时对象。
std::move
实现,非常类似于Scott Meyers在Effective Modern C++中展示的(在书中,返回类型std::remove_reference_t<T>&&
被改为decltype(auto)
,它从return
语句中推导出来)。template<typename T>
std::remove_reference_t<T>&& move(T&& t) {
return static_cast<std::remove_reference_t<T>&&>(t);
}
std::move
的以下内容:
T
上工作;T&&
接收其唯一参数,因此可以操作左值和右值;T
将相应地被推断为左值引用或非引用类型;<…>
指定模板参数,在实践中,您应该永远不要指定它;std::move
只是一个使用根据推断出的非模板参数类型自动确定的模板参数的static_cast
;std::remove_reference_t
从T
中去除任何引用性,然后添加&&
来实现。<utility>
中的std::move
之外,还有另一个吗?是的,它就是<algorithm>
中的std::move
,它做了一个半相关的事情:它是std::copy
的一个版本,不是将值从一个容器复制到另一个容器,而是移动它们,使用<utility>
中的std::move
;所以它是使用另一个std::move
的std::move
!
std::forward
std::forward
用于将函数内的参数转发到另一个函数,并告诉后者前者是否使用临时对象调用。
std::forward<X>(x)
有两种含义:
x
绑定到一个rvalue,即临时对象) 嗨,函数先生,我从另一个函数那里收到了这个包裹,在你处理完它后,他不再需要它,所以请随意对待它;x
绑定到一个lvalue,即非临时对象) 嗨,函数先生,我从另一个函数那里收到了这个包裹,在你处理完它后,他还需要它,所以请不要破坏它。因此,通常在转发/通用引用上使用它,因为它们可以绑定到临时对象和非临时对象。
换句话说,std::forward
用于能够转换这段代码
template<typename T>
void wrapper(T&& /* univ. ref.: it binds to lvalues as well as rvalues (temporaries)*/ t) {
// here `t` is an lvalue, so it doesn't know whether it is bound to a temporary;
// `T` encodes this missing info, but sadly we're not making `some_func` aware of it,
// therefore `some_func` will not be able to steal resources from `t` if `t`
// is bound to a temporary, because it has to leave lvalues intact
some_func(t);
}
template<typename T>
void wrapper(T&& /* univ. ref.: it binds to lvalues as well as rvalues (temporaries)*/ t) {
// here `t` is an lvalue, so it doesn't know whether it is bound to a temporary;
// `T` encodes this missing info, and we do use it:
// `t` bound to lvalue => `T` is lvalue ref => `std::forward` forwards `t` as lvalue
// `t` bound to rvalue => `T` is non-ref => `std::forward` turns `t` into rvalue
some_func(std::forward<T>(t));
}
std::forward
。template<typename T>
T&& forward(std::remove_reference_t<T>& t) {
return static_cast<T&&>(t);
}
std::forward
的以下内容:
T
;T
的唯一参数;请注意,由于引用折叠(参见这里),std::remove_reference_t<T>&
解析为与T&
完全相同的内容;然而...std::remove_reference_t<T>&
而不是T&
,正是为了将T
放在无法推导的上下文中(参见这里),从而禁用模板类型推导,因此您必须通过<…>
来指定模板参数std::forward
只是一个使用模板参数(通过引用折叠)自动确定的static_cast
;T&&
的引用折叠,其中T
是您传递给std::forward
的模板参数:如果T
是非引用类型,则T&&
是rvalue引用,而如果T
是lvalue引用,则T&&
也是lvalue引用;static_cast<T&&>
代替std::forward<T>
,因为它们是等效的,并且可以节省一个实例化,但代码不够清晰,可能无法捕获错误。
std::move
无条件地将其参数转换为右值
std::forward
用于将参数以与传递给函数时完全相同的方式进行“转发”。 就像这里所示:
使用std::move
将对象作为右值提供,以可能匹配移动构造函数或接受右值的函数。即使x
本身不是右值,也会对std::move(x)
执行此操作。
move
,当您想要使用完美转发时,请使用forward
。这并不是什么高深的科学 ;) - Nicol Bolas