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 auto in C++ to avoid redundant type name duplication

2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Today, I'm going to talk to you about how to use auto in C++ to avoid redundant type name duplications. maybe many people don't know much about it. In order to make you understand better, the editor summarized the following. I hope you can get something from this article.

Reason (reason)

Simple repetition is tedious and error-prone.

Simple repetition is redundant and error-prone.

When you use auto, the name of the declared entity is in a fixed position in the declaration, increasing readability.

When you use auto, the name of the defined entity appears in a fixed location, which increases readability.

In a template function declaration the return type can be a member type.

In a template function definition, the return value can be a member type.

Example (sample)

Consider:

Consider the following code:

Auto p = v.begin (); / / vector::iterator

Auto h = t.future ()

Auto Q = make_unique (s)

Auto f = [] (int x) {return x + 10;}

In each case, we save writing a longish, hard-to-remember type that the compiler already knows but a programmer could get wrong.

In either case, we don't have to write type information that is long and difficult to remember. In fact, the compiler already knows this information, but programmers can still make mistakes.

Example (sample)

Template

Auto Container::first ()-> Iterator; / / Container::IteratorException (exception)

Avoid auto for initializer lists and in cases where you know exactly which type you want and where an initializer might require conversion.

For cases where you know exactly what type you want but the initializer may need to convert, avoid using auto for initialization lists.

Example (sample)

Auto lst = {1,2,3}; / / lst is an initializer list

Auto x {1}; / / x is an int (in Clippers 17; initializer_list in Clippers 11) Note (note)

When concepts become available, we can (and should) be more specific about the type we are deducing:

After concepts can, we can (and should) be more specific about the types we infer.

/ /...

ForwardIterator p = algo (x, y, z); Example (Clear17)

Example (Category 17)

Auto [quotient, remainder] = div (123456, 73); / / break out the members of the div_t resultEnforcement (implementation recommendation)

Flag redundant repetition of type names in a declaration.

Redundant type name duplicates that occur when the tag is declared.

After reading the above, do you have any further understanding of how to use auto in C++ to avoid redundant type name repetition? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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