[英]Can I typically/always use std::forward instead of std::move?
I've been watching Scott Meyers' talk on Universal References from the C++ and Beyond 2012 conference, and everything makes sense so far. 我一直在观看Scott Meyers 关于 C ++和2012 年之后的Universal References的演讲,到目前为止一切都很有意义。 However, an audience member asks a question at around 50 minutes in that I was also wondering about.
然而,一位观众在大约50分钟后问了一个问题,我也在想。 Meyers says that he does not care about the answer because it is non-idiomatic and would silly his mind, but I'm still interested.
迈尔斯说,他不关心答案,因为它不是惯用的,会愚蠢的想法,但我仍然感兴趣。
The code presented is as follows: 提供的代码如下:
// Typical function bodies with overloading:
void doWork(const Widget& param) // copy
{
// ops and exprs using param
}
void doWork(Widget&& param) // move
{
// ops and exprs using std::move(param)
}
// Typical function implementations with universal reference:
template <typename T>
void doWork(T&& param) // forward => copy and move
{
// ops and exprs using std::forward<T>(param)
}
The point being that when we take an rvalue reference, we know we have an rvalue, so we should std::move
it to preserve the fact that it's an rvalue. 关键是当我们采用右值引用时,我们知道我们有一个rvalue,所以我们应该
std::move
它来保留它是一个rvalue的事实。 When we take a universal reference ( T&&
, where T
is a deduced type), we want std::forward
to preserve the fact that it may have been an lvalue or an rvalue. 当我们采用通用引用(
T&&
,其中T
是推导类型)时,我们希望std::forward
保留它可能是左值或右值的事实。
So the question is: since std::forward
preserves whether the value passed into the function was either an lvalue or an rvalue, and std::move
simply casts its argument to an rvalue, could we just use std::forward
everywhere? 所以问题是:因为
std::forward
保留传递给函数的值是左值还是右值,而std::move
只是将其参数转换为右值,我们可以只使用std::forward
吗? Would std::forward
behave like std::move
in all cases where we would use std::move
, or are there some important differences in behaviour that are missed out by Meyers' generalisation? 在我们使用
std::move
所有情况下, std::forward
行为都会像std::move
一样,还是在Meyers的泛化中错过了一些重要的行为差异?
I'm not suggesting that anybody should do it because, as Meyers correctly says, it's completely non-idiomatic, but is the following also a valid use of std::move
: 我并不是说任何人都应该这样做,因为正如迈耶斯所说,这完全是非惯用的,但以下也是
std::move
的有效用法:
void doWork(Widget&& param) // move
{
// ops and exprs using std::forward<Widget>(param)
}
The two are very different and complementary tools. 这两者是非常不同和互补的工具。
std::move
deduces the argument and unconditionally creates an rvalue expression. std::move
推导出参数并无条件地创建一个rvalue表达式。 This makes sense to apply to an actual object or variable. 这适用于实际对象或变量。
std::forward
takes a mandatory template argument (you must specify this!) and magically creates an lvalue or an rvalue expression depending on what the type was (by virtue of adding &&
and the collapsing rules). std::forward
采用强制模板参数(你必须指定它!)并根据类型(通过添加&&
和折叠规则)神奇地创建一个左值或右值表达式。 This only makes sense to apply to a deduced, templated function argument. 这仅适用于推导的,模板化的函数参数。
Maybe the following examples illustrate this a bit better: 也许以下示例说明了这一点:
#include <utility>
#include <memory>
#include <vector>
#include "foo.hpp"
std::vector<std::unique_ptr<Foo>> v;
template <typename T, typename ...Args>
std::unique_ptr<T> make_unique(Args &&... args)
{
return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); // #1
}
int main()
{
{
std::unique_ptr<Foo> p(new Foo('a', true, Bar(1,2,3)));
v.push_back(std::move(p)); // #2
}
{
v.push_back(make_unique<Foo>('b', false, Bar(5,6,7))); // #3
}
{
Bar b(4,5,6);
char c = 'x';
v.push_back(make_unique<Foo>(c, b.ready(), b)); // #4
}
}
In situation #2, we have an existing, concrete object p
, and we want to move from it, unconditionally. 在情况#2中,我们有一个现有的具体对象
p
,我们想要无条件地从它移动。 Only std::move
makes sense. 只有
std::move
才有意义。 There's nothing to "forward" here. 这里没有什么可以“前进”的。 We have a named variable and we want to move from it.
我们有一个命名变量,我们希望从中移动。
On the other hand, situation #1 accepts a list of any sort of arguments, and each argument needs to be forwarded as the same value category as it was in the original call. 另一方面,情境#1接受任何类型的参数列表,并且每个参数需要作为与原始调用中相同的值类别转发。 For example, in #3 the arguments are temporary expressions, and thus they will be forwarded as rvalues.
例如,在#3中,参数是临时表达式,因此它们将作为rvalues转发。 But we could also have mixed in named objects in the constructor call, as in situation #4, and then we need forwarding as lvalues.
但是我们也可以在构造函数调用中混合使用命名对象,如情况#4,然后我们需要转发为左值。
Yes, if param
is a Widget&&
, then the following three expressions are equivalent (assuming that Widget
is not a reference type): 是的,如果
param
是一个Widget&&
,那么以下三个表达式是等价的(假设Widget
不是引用类型):
std::move(param)
std::forward<Widget>(param)
static_cast<Widget&&>(param)
In general (when Widget
may be a reference), std::move(param)
is equivalent to both of the following expressions: 通常(当
Widget
可能是引用时), std::move(param)
等效于以下两个表达式:
std::forward<std::remove_reference<Widget>::type>(param)
static_cast<std::remove_reference<Widget>::type&&>(param)
Note how much nicer std::move
is for moving stuff. 注意
std::move
对于移动东西有多好。 The point of std::forward
is that it mixes well with template type deduction rules: std::forward
是它与模板类型扣除规则很好地混合:
template<typename T>
void foo(T&& t) {
std::forward<T>(t);
std::move(t);
}
int main() {
int a{};
int const b{};
//Deduced T Signature Result of `forward<T>` Result of `move`
foo(a); //int& foo(int&) lvalue int xvalue int
foo(b); //int const& foo(int const&) lvalue int const xvalue int const
foo(int{});//int foo(int&&) xvalue int xvalue int
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.