Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use the std::forward of C++

Shulou Source: shulou.com Published: 2022-06-01 19:30:43 10月03日 Update

This article mainly explains "how to use C++ 's std::forward". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use C++ 's std::forward.

For parameters that are passed only but not handled, use the template type TP&& and use std::forwardReason when passing (reason)

If the object is to be passed onward to other code and not directly used by this function, we want to make this function agnostic to the argument const-ness and rvalue-ness.

If the object is not used within this function but continues to be passed to other code, we hope that this function will not change the constant and right value properties of the parameter.

Translator's note:

Consider the following functions:

String f (string&& s) {if (s.size ()) s [0] = toupper (s [0]); return s}

When the right value reference is used as a parameter type, it is first initialized by the argument, and the result is that the argument is invalidated (the definition of the right value reference). Inside the function, because s has captured the contents of the argument, it can be used freely as a left value. But if you do not use it directly in the function body and want to continue to pass it to another function as a right value, you cannot directly use s as an argument (because it has become a left value reference). Instead, you can use forward to restore its right value property.

In this case, it is only in this case (the right reference parameter is passed but not used) that the TP parameter is defined as TP&& (where TP is the template type)-- this ignores and maintains the constant and right-value properties. So any code that uses TP&& declares that it doesn't care about the constant and right properties of the variable (because it has been ignored), but continues to pass that value (unchanged) to other code that doesn't care about the constant and right properties (because these properties are maintained). Because any temporary object passed from the caller remains valid during the function call (because the caller has a chance to destroy the object only after the function call), it is safe when TP&& is used as a parameter (inside the function). Parameters of type TP&& should essentially always be passed through std::forward in the body of the function.

Translator's note: eventually, it has to be used as a left value by a piece of code.

Exampletemplate inline auto invoke (F f, Args&&...) Args) {return f (forward (args)...);} at this point, I believe you have a better understanding of "how to use C++ 's std::forward". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Functions attributes parameters code types clocking content objects constants causes only cases templates users but translators learning practical deeper effective Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info OPPO Reno Docker Redmi Huawei