[英]Using std::forward for other than forwarding args to another function
I have a number of methods of the following form 我有以下几种形式的方法
foo(std::string const & name)
if (name.empty()) {...}
I was trying to follow Scott Meyers' note on using universal references and std::forward to make a function that efficiently takes a number of string forms. 我试图遵循Scott Meyers关于使用通用引用和std :: forward的说明,以制作一个有效地采用多种字符串形式的函数。
From example, I understand how to use std::forward to forward a function argument to another function, but what do I do about the name.empty() expression? 从示例中,我了解了如何使用std :: forward将函数参数转发给另一个函数,但是对name.empty()表达式我该怎么办?
I think the answer is std::forward(name).empty() but I can find any documentation that actually says that. 我认为答案是std :: forward(name).empty(),但我可以找到任何实际表明这一点的文档。
Use std::forward
only when you actually need to forward the value category. 仅在确实需要转发值类别时才使用
std::forward
。 That is, std::forward
is used to guarantee that if your function is called with an lvalue argument then it will call another function with an lvalue argument, and if your function is called with an rvalue argument then it will call another function with an rvalue argument. 也就是说,
std::forward
用于确保如果使用左值参数调用您的函数,则它将使用左值参数调用另一个函数,如果使用右值参数调用了您的函数,则它将调用带有左值参数的另一个函数。右值参数。
In the case where you want to check whether a string is empty, it doesn't matter whether the argument was an lvalue or rvalue---the string::empty
function should not behave differently. 在要检查字符串是否为空的情况下,参数是左值还是右值都没关系-string
string::empty
函数的行为不应有所不同。 So you don't need std::forward
. 因此,您不需要
std::forward
。 Instead, just name.empty()
suffices. 相反,仅
name.empty()
就足够了。
There is an easy way to figure out when to use std::forward
. 有一种简单的方法可以弄清楚何时使用
std::forward
。
Imagine your incoming argument was, instead of a T&& t
just a T t
-- a value parameter instead of a forwarding reference. 想象一下,您传入的参数不是
T&& t
,而是T t
值参数,而不是转发引用。 If you would call std::move(t)
in the second case, calling std::forward<T>(t)
in the first case is a good idea. 如果在第二种情况下调用
std::move(t)
,则在第一种情况下调用std::forward<T>(t)
是一个好主意。
std::forward
is a conditional move. std::forward
是有条件的动作。 It will invoke the move
iff the reference was an rvalue passed in, and will do mostly nothing if it wasn't an rvalue. 如果引用是传入的右值,它将调用
move
,如果不是右值,则几乎不执行任何操作。
The next question is, when should you move
a value parameter? 下一个问题是,什么时候应该
move
值参数?
Well, the first general rule is you should move a value parameter the last time you'll ever refer to it in its scope. 好吧,第一个一般规则是您应该在上次在其范围内引用它时才移动值参数。 You are marking its state as "scratch" and that it be recycled in that last operation.
您将其状态标记为“暂存”,并且在最后一个操作中将其回收。
There are a few other places to use move
. 还有其他一些使用
move
地方。
If you are breaking it down into individual components (like the parts of a tuple), and you know the access will be isolated to the part ( std::get<N>
, or .member
), and there are no invariants that will be violated, then move
will tell the code that the component can be torn out of the object. 如果您将其分解为各个组成部分(例如元组的各个部分),并且您知道访问将被隔离到该部分(
std::get<N>
或.member
),则不会有不变量被违反,然后move
将告诉代码该组件可以从对象中撕下。 You can end up calling move
on the same variable multiple times in this sense, but each time you are conceptually only moving one part of it. 从这种意义上讲,您可能最终多次调用同一变量上的
move
,但是从概念上讲,每次您只移动它的一部分。
This is sometimes distinct from calling move
on the component itself, as std::get<i>(std::move(foo))
will behave differently than std::move( std::get<i>(foo) )
when foo
contains reference parameters. 有时这与在组件本身上调用
move
有所不同,因为std::get<i>(std::move(foo))
行为与std::move( std::get<i>(foo) )
有所不同foo
包含参考参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.