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 using in Category 11

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

Share

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

This article mainly introduces how to use using in Craft 11, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let's take a look at it.

1. Define alias

In C++, you can redefine a type through typedef. The syntax format is as follows:

Typedef old type name new type name; / / use example typedef unsigned int uint_t

The redefined type is not a new type, it's just a new name for the original type. As with previous declaration statements, the declarants here can also contain type modifiers, so that compound types can also be constructed from basic data types. A new way of defining aliases for types is defined using alias declarations (alias declaration), which is called using, as specified in Category declaration 11.

When used, the keyword using begins the alias declaration, followed by an alias and an equal sign to define the name to the left of the equal sign as an alias of the type to the right of the equal sign. A type alias is equivalent to the name of a type, and a type alias can be used wherever the name of the type can appear. Aliases defined using typedef and aliases defined using using are semantically equivalent.

The syntax format for defining aliases using using is as follows:

Using new type = old type; / / use example using uint_t = int

From the syntax format of using and typedef, we can see that there is not much difference between the two. Suppose we define a function pointer, the advantage of using can be highlighted. Take a look at the following example:

/ define function pointer typedef int (* func_ptr) (int, double) using typedef; / / define function pointer using func_ptr1 = int (*) (int, double) using using

If you are not particularly familiar with function pointers and typedef, it is difficult to see at first glance that func_ptr is actually an alias, its essence is a function pointer, the function return type is int, and the function arguments have two int,double types.

Using using to define the function pointer alias looks very intuitive, forcing the alias name to be separated to the left, and putting the actual type of alias on the right, which is clearer and more readable.

two。 Alias of the template

It is very convenient to use typedef redefinition, but it has some limitations. For example, we cannot redefine a template. For example, we need a map with a fixed int type as key, which can be mapped with many types of value values. It is very troublesome to define it directly using typedef:

Typedef map M1 map typedef map m2transfertypedef map m3

In this case, we unconsciously think of the template:

Template typedef map type; / / error, syntax error

Using typename does not support defining aliases for templates. This simple requirement is difficult to do only through typedef. You need to add an external class:

# include # include # include using namespace std;template / / defines external application classes struct MyMap {typedef map type;}; int main (void) {MyMap::type m; m.insert (make_pair (1, "luffy")); m.insert (make_pair (2, "ace")); MyMap::type M1; m1.insert (1,100); m1.insert (2,200); return 0;}

Through the above example, we can intuitively feel that the requirements are simple but not easy to implement. A new feature in Clover 11 is the ability to define aliases for a template by using using, and the above requirements can be written as follows:

Template using mymap = map

The complete sample code is as follows:

# include # include # include using namespace std;template using mymap = map;int main (void) {/ / value of map is specified as string type mymap m; m.insert (make_pair (1, "luffy")); m.insert (make_pair (2, "ace")); / / value of map is specified as int type mymap M1; m1.insert (1,100); m1.insert (2,200); return 0;}

In the above example, by using using to assign an alias to the template, you can easily assign the corresponding type to value based on the alias, which makes the program more flexible and looks more concise.

Finally, I would like to emphasize that using syntax, like typedef, does not create new types, they just define new aliases for some types. The advantage of using over typedef is that it looks more intuitive when defining function pointer aliases and can define aliases for templates.

Thank you for reading this article carefully. I hope the article "how to use using in Category 11" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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