Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use auto in C++ to avoid redundant type name duplication

Shulou Source: shulou.com Published: 2022-06-02 05:51:14 09月20日 Update

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.

Tags: Type example content caching information name case code location function reason readability entity suggestion member time more tag template knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Redmi Apple Xiaomi Huawei