我可以通常/总是使用std :: forward而不是std :: move? [英] Can I typically/always use std::forward instead of std::move?
问题描述
我一直在看Scott Meyers的在C ++和Beyond 2012会议上讨论通用引用,到目前为止,一切都有意义。然而,观众成员在大约50分钟时问一个问题,我也想知道。 Meyers说他不关心答案,因为它是非惯用的,会愚蠢他的心,但我仍然感兴趣。
I've been watching Scott Meyers' talk on Universal References from the C++ and Beyond 2012 conference, and everything makes sense so far. However, an audience member asks a question at around 50 minutes in that I was also wondering about. 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.
代码如下:
// 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)
}
注意,当我们采用右值引用时,我们知道我们有一个右值,所以我们应该 std :: move
它保留这是一个右值的事实。当我们采用通用引用( T&&&
,其中 T
是推导类型)时, c $ c> std :: forward 以保留它可能是一个左值或右值的事实。
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. 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.
所以问题是:因为 std :: forward
保存传递给函数的值是否为左值或右值, std :: move
简单地将其参数转换为右值,我们可以使用 std :: forward
everywhere吗?在所有使用的情况下,
,或者在Meyers的概括中漏掉了一些重要的行为差异? std :: forward
的行为如 std :: move
std :: move
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? 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?
我不建议任何人都应该这样做,因为正如Meyers正确地说,它完全是非惯用的,但下面也是 std :: move
的有效使用:
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
:
void doWork(Widget&& param) // move
{
// ops and exprs using std::forward<Widget>(param)
}
推荐答案
两个是非常不同的和互补工具。
The two are very different and complementary tools.
-
std :: move
推断参数,并无条件创建一个右值表达式。这适用于实际的物件或变数。
std::move
deduces the argument and unconditionally creates an rvalue expression. This makes sense to apply to an actual object or variable.
std :: forward
模板参数(你必须指定这个!),并神奇地创建一个左值引用或右值表达式,取决于类型是什么(通过添加&&
规则)。这只适用于推导的模板函数参数。
std::forward
takes a mandatory template argument (you must specify this!) and magically creates an lvalue reference or an rvalue expression depending on what the type was (by virtue of adding &&
and the collapsing rules). 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
}
}
一个现有的,具体的对象 p
,我们想从它,无条件。只有 std :: move
才有意义。没有什么可以转发在这里。我们有一个命名的变量,我们想从它移动。
In situation #2, we have an existing, concrete object p
, and we want to move from it, unconditionally. Only std::move
makes sense. There's nothing to "forward" here. We have a named variable and we want to move from it.
另一方面,情况#1接受任何类型的参数的列表,每个参数需要被转发为与原始呼叫中相同的值类别。例如,在#3中,参数是临时表达式,因此它们将作为右值转发。但我们也可以在构造函数调用中混合命名对象,如情况#4,然后我们需要转发为lvalues。
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. For example, in #3 the arguments are temporary expressions, and thus they will be forwarded as 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.
这篇关于我可以通常/总是使用std :: forward而不是std :: move?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!