Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of universal reference and right value reference in Clear11

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of the methods that C++ 110000 can quote and right value quotation, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe that after reading this C++ 110000 can quote and right value citation method article will have a harvest, let's take a look.

Text

In fact, type&& has two different meanings.

One of them is the right value reference. It only binds the right value and is used to identify removable objects.

Another meaning means that it can be either a right value reference or a left value reference. This dual feature allows it to be bound to either the right value or the left value. You can also bind to const objects or non-const objects, as well as volatile objects, or even those with both const and volatile, with great flexibility, which is called universal reference.

So, since both of the above meanings are type&& structures, how do you distinguish between the two?

Universal citation

Universal references usually appear in two scenarios: the formal parameters of the function template and the auto declaration.

Examples are as follows:

Template void f (var2 & param); / / param is a universal reference auto & & var2 = var1

What these two scenarios have in common is that they both involve type derivation.

In template f, the type of param is derived, and in the declaration statement of var2, the type of var2 is also derived.

Because a universal reference is first and foremost a reference, initialization is necessary. The initializer of the universal reference will determine whether it represents a left value or a right value reference. if the initializer is a left value, the universal reference will get a left value reference. Similarly, if the initializer is a right value, the universal reference will get a right value reference.

For a universal reference as a function parameter, the initializer provides at the call:

Template void f (std::move & param); Widget wtransf (w); / / the type in which the left value is passed to fMagazine param is Widget&, that is, the left value reference f (std::move (w)); / / the type in which the right value is passed to fMagazine param is Widget&&, that is, right value reference

One thing to note is that in addition to type derivation, universal citation must be limited, that is, it must be a "Tunable &" structure.

And similar

Template void f (std::vector&& param); / / param is a right value reference

Such a type is not a universal reference, it is just a right value reference.

Moreover, it is impossible to become a universal reference if there is const modification, such as:

Template void f (const checking & param); / / param is the right value reference

So, does it necessarily involve type derivation if it is located in the template? There's no guarantee. Look at the following example:

Template class vector {public: void push_back (tipped & x);...}

The above is from the vector class in the C++ standard

The formal parameter of push_back here has the correct form of universal reference, but in this example, type derivation is not involved. Because push_back is part of the vector itself, it cannot exist if there is no specific instance of vector. The specific type of the instance completely determines the declaration type of the push_back.

Such as:

Std::vector v

It will cause the std::vector template to materialize as the following example:

Template class vector {public: void push_back (Widget&& x); / / right value reference.}

It is now clear that push_back does not involve type inference.

Another function in vector involves type derivation, as follows:

Template class vector {public: template void emplace_back (Args&&... Args);.}; on "C++ 110000 can be referenced and right-value reference methods" this article is introduced here, thank you for reading! I believe you all have a certain understanding of the knowledge of "C++ 110000 can be quoted and right-value quotes". If you want to learn more knowledge, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report