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

How to use the forward function in Clipper 11

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

The purpose of this article is to share with you about how to use the forward function in Category 11. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.

Value transfer function template

Suppose you have a set of functions that work based on two input values, such as the following add function:

We want to write a function template to call these functions.

The use of this function template is as follows:

The programmer can modify the function name parameter and the type of the two variables as needed. It's been a warm-up so far.

The left value references the parameter function template

In addition to functions like the add function, where both arguments are input, you may want to call such a function:

In these functions, you want to output the results of the operation by referencing the type. In this case, you can define the function template as follows:

The use of function template 2 is as follows:

This function template can use both add and inc functions. But there is one small problem: there is no way to use a constant as a parameter as in the first case, because the constant is a right value.

The right value references the parameter function template

In order to solve the problem of the left value referencing the parameter template, you can define the right value reference parameter function template.

With this template, the following code can be compiled and executed normally.

It is not a problem to specify a constant where the parameters are entered. It's already pretty good.

Problems in the process of progress

What if we want this template function to call the following function?

There is no way to compile the following code.

The compilation error is that int cannot be converted to int&& type when swap_rvalue is called.

What happened

In order to find out what's going on inside the function template, the author wrote the following function:

The function of this function is to get the actual type of type T mentioned in this article. Although incomplete, it can be used to illustrate the problems encountered in this article. This function can be used as follows:

The output is as follows:

It correctly outputs the types of int,int& and int&&. We can embed showType into the function template:

To avoid compilation errors, block the part of the calling function for a while. The output of the template function is:

As a result of the reference merge described in the previous article, the type of T1Magine T2 becomes the right value, respectively. This is why the call to swap_int failed.

The solution of Clear11

In order to solve the problem of parameter type changes in template functions, std::forward can be used to restore the left or right value attributes of template parameters. The modified code is as follows:

Before calling f, two additional lines of code are added to represent the execution result of the std::foward. The program output is as follows:

As you can see, the type at the time of the call is successfully restored and the swap_int function is executed correctly. Not only that, the following code is executed correctly.

The above is how to use the forward function in Clipper 11. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please 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

Internet Technology

Wechat

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

12
Report