简体   繁体   English

除了将args转发到另一个函数外,还使用std :: forward

[英]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.

相关问题 当 function 接受对另一个 function 的转发引用时,使用 std::forward 调用 function 的影响是什么 - When a function accepts a forwarding reference to another function, what is the impact of calling the function using std::forward 使用带转发引用的std :: forward - Using std::forward with a forwarding reference 完美转发`decltype(std :: forward <Args>(args))...`和Args &&之间的区别是什么? - In perfect forwarding what is the difference between `decltype(std::forward<Args>(args))…` and Args&& 使用两个以上的arg推导std :: function - Deducing std::function with more than two args 将 arguments 转发到另一个模板 function 会导致错误 C2665:“std::forward”:2 个重载中没有一个可以转换所有参数类型 - Forwarding arguments to another template function results in error C2665: 'std::forward': none of the 2 overloads could convert all the argument types 为什么在使用转发引用时需要 std::forward? - Why is std::forward needed when using forwarding references? 将std :: forward与非转发的普通旧引用一起使用 - Using std::forward with a non-forwarding, plain old reference std :: forward没有完美的转发? - std::forward without perfect forwarding? 使用 std::forward 转发重载函数 - Overloading Functions with std::forward Forwarding 使用转发引用时,std :: move是否需要与std :: forward结合使用? - Does std::move need to be combined with std::forward when using forwarding references?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM