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.
我一直在看Scott Meyers关于c++以及2012年以后的通用引用的演讲,到目前为止,一切都很有意义。然而,一位听众在大约50分钟的时候问了一个问题,我也在想这个问题。迈耶斯说,他并不在意答案,因为它是非惯用的,会让他头脑发呆,但我还是很感兴趣。
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. 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.
关键是当我们使用一个rvalue引用时,我们知道我们有一个rvalue,所以我们应该std::移动它以保持它是一个rvalue的事实。当我们使用一个通用引用(T&&,其中T是一种推断类型)时,我们希望std::forward保留它可能是lvalue或rvalue的事实。
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?
问题是:既然std::forward保留了传递给函数的值是lvalue还是rvalue,而std::move只是将它的参数转换为rvalue,我们是否可以在所有地方都使用std:::forward ?在所有我们使用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
:
我并不是说每个人都应该这么做,因为迈耶斯说得对,这完全不是习惯用语,但以下也是std的有效用法:
void doWork(Widget&& param) // move
{
// ops and exprs using std::forward<Widget>(param)
}
2 个解决方案
#1
67
The two are very different and complementary tools.
这两者是非常不同的、互补的工具。
-
std::move
deduces the argument and unconditionally creates an rvalue expression. This makes sense to apply to an actual object or variable.move演绎参数并无条件地创建一个rvalue表达式。这对于应用于实际对象或变量是有意义的。
-
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.forward接受一个必须的模板参数(您必须指定它!)并根据类型创建一个lvalue引用或一个rvalue表达式(通过添加&&和崩溃规则)。这只适用于演绎的模板化函数参数。
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. Only std::move
makes sense. There's nothing to "forward" here. We have a named variable and we want to move from it.
在情形2中,我们有一个现有的,具体的对象p,我们想要从它开始,无条件地。只有std::行动是有意义的。这里没有什么可以“前进”的。我们有一个命名变量,我们想从它开始。
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.
另一方面,情形1接受任何类型的参数列表,每个参数都需要作为与原始调用中相同的值类别进行转发。例如,在#3中,参数是临时表达式,因此它们将被作为rvalues转发。但是我们也可以在构造函数调用中混合命名对象,如情况#4,然后我们需要转发作为lvalues。
#2
14
Yes, if param
is a Widget&&
, then the following three expressions are equivalent (assuming that Widget
is not a reference type):
是的,如果param是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:
一般情况下(当小部件可能是一个引用)时,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. The point of std::forward
is that it mixes well with template type deduction rules:
注意:move是用来移动东西的。std的要点是:它与模板类型的演绎规则很好地结合在一起:
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
}
#1
67
The two are very different and complementary tools.
这两者是非常不同的、互补的工具。
-
std::move
deduces the argument and unconditionally creates an rvalue expression. This makes sense to apply to an actual object or variable.move演绎参数并无条件地创建一个rvalue表达式。这对于应用于实际对象或变量是有意义的。
-
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.forward接受一个必须的模板参数(您必须指定它!)并根据类型创建一个lvalue引用或一个rvalue表达式(通过添加&&和崩溃规则)。这只适用于演绎的模板化函数参数。
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. Only std::move
makes sense. There's nothing to "forward" here. We have a named variable and we want to move from it.
在情形2中,我们有一个现有的,具体的对象p,我们想要从它开始,无条件地。只有std::行动是有意义的。这里没有什么可以“前进”的。我们有一个命名变量,我们想从它开始。
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.
另一方面,情形1接受任何类型的参数列表,每个参数都需要作为与原始调用中相同的值类别进行转发。例如,在#3中,参数是临时表达式,因此它们将被作为rvalues转发。但是我们也可以在构造函数调用中混合命名对象,如情况#4,然后我们需要转发作为lvalues。
#2
14
Yes, if param
is a Widget&&
, then the following three expressions are equivalent (assuming that Widget
is not a reference type):
是的,如果param是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:
一般情况下(当小部件可能是一个引用)时,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. The point of std::forward
is that it mixes well with template type deduction rules:
注意:move是用来移动东西的。std的要点是:它与模板类型的演绎规则很好地结合在一起:
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
}