Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use static_cast to convert a left value to a right value

Shulou Source: shulou.com Published: 2022-06-01 19:24:51 09月20日 Update

This article mainly explains "how to convert the left value to the right value with static_cast". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to convert the left value to the right value with static_cast".

Use std::move

Examine the following two functions:

It is the same except that one of the parameter types is a left value reference and the other is a right value reference. Combined with the previous article, we can draw the following conclusion: the left value reference indicates that the value may be modified, and the right value reference is the right to take over the parameter data. Accordingly, in the case of a reference to the right value, the caller is required to explicitly waive the right.

The way to waive your rights here is through the std::move function, as you did in the fr function in the figure above.

Understand std::move

Here is the source code for std::move:

Looking at the function body first, you only do one thing: use static_cast to explicitly convert argument t to another type, which is:

Typename is used to indicate that the following expression is a type rather than a variable, ignore it first, then remove the negative & & (forget about it for the time being), and the code looks like this:

We know (and will let you know if we don't know) that the function of remove_reference is to remove reference type modifiers.

For example: return int; if the type of T is int& or int&&, and return int if the type of T is int

Considering the & & modifier you just ignored, you can conclude that the target type of static_cast is the input type T, which removes possible reference modifiers, and then adds right-value reference modifiers.

In other words, the function of std::move is to use static_cast to explicitly convert T.m.m.T.T.m.m.cn.

Thank you for your reading, the above is the content of "how to convert the left value to the right value with static_cast". After the study of this article, I believe you have a deeper understanding of how to convert the left value to the right value with static_cast. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Type function right learning content function parameter that is situation article conclusion face and then the figure above two that is code all variables. Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Linux Shulou Tech Info Docker macOS