Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6210571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:11:01+00:00 2026-05-24T06:11:01+00:00

Possible Duplicate: How much is too much with C++0x auto keyword Have we (as

  • 0

Possible Duplicate:
How much is too much with C++0x auto keyword

Have we (as a community) had enough experience to determine when and/or whether auto is being abused?

What I am really looking for is a best practices guide on

  • when to use auto
  • when it should be avoided

Simple rules of thumb that can quickly be followed in 80% of cases.

As a context this question is sparked by my response here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T06:11:02+00:00Added an answer on May 24, 2026 at 6:11 am

    I think when the type is very well-known amongst the co-programmers who work (or would work) in your project, then auto can be used, such as in the following code:

    //good : auto increases readability here
    for(auto it = v.begin(); it != v.end(); ++it) //v is some [std] container
    {
          //..
    }
    

    Or, more generally,

    //good : auto increases readability here
    for(auto it = std::begin(v); it != std::end(v); ++it)//v could be array as well
    {
          //..
    }
    

    But when the type is not very well-known and infrequently used , then I think auto seems to reduce readability, such as here:

    //bad : auto decreases readability here
    auto obj = ProcessData(someVariables);
    

    While in the former case, the usage of auto seems very good and doesn’t reduce readability, and therefore, can be used extensively, but in the latter case, it reduces readabilty and hence shouldn’t be used.


    Another place where auto can be used is when you use new1 or make_* functions , such as here:

    //without auto. Not that good, looks cumbersome
    SomeType<OtherType>::SomeOtherType * obj1 = new SomeType<OtherType>::SomeOtherType();
    std::shared_ptr<XyzType> obj2 = std::make_shared<XyzType>(args...);
    std::unique_ptr<XyzType> obj2 = std::make_unique<XyzType>(args...);
    
    //With auto. good : auto increases readability here
    auto obj1 = new SomeType<OtherType>::SomeOtherType();
    auto obj2 = std::make_shared<XyzType>(args...);
    auto obj3 = std::make_unique<XyzType>(args...);
    

    Here it is very good, as it reduces the use of keyboard, without reducing the readability, as anyone can know the type of objects being created, just by looking at the code.

    1. Avoid using new and raw-pointers though.


    Sometime, the type is so irrelevant that the knowledge of the type is not even needed, such as in expression template; in fact, practically it is impossible to write the type (correctly), in such cases auto is a relief for programmers. I’ve written expression template library which can be used as:

    foam::composition::expression<int> x;
    
    auto s = x * x;       //square
    auto c = x * x * x;   //cube
    for(int i = 0; i < 5 ; i++ )
        std::cout << s(i) << ", " << c(i) << std::endl; 
    

    Output:

    0, 0
    1, 1
    4, 8
    9, 27
    16, 64
    

    Now compare the above code with the following equivalent code which doesn’t use auto:

    foam::composition::expression<int> x;
    
    //scroll horizontally to see the complete type!!
    foam::composition::expression<foam::composition::details::binary_expression<foam::composition::expression<int>, foam::composition::expression<int>, foam::operators::multiply>> s = x * x; //square
    foam::composition::expression<foam::composition::details::binary_expression<foam::composition::expression<foam::composition::details::binary_expression<foam::composition::expression<int>, foam::composition::expression<int>, foam::operators::multiply> >, foam::composition::expression<int>, foam::operators::multiply>> c = x * x * x; //cube
    
    for(int i = 0; i < 5 ; i++ )
        std::cout << s(i) << ", " << c(i) << std::endl; 
    

    As you can see, in such cases auto makes your life exponentially easier. The expressions used above are very simple; think about the type of some more complex expressions:

    auto a = x * x - 4 * x + 4; 
    auto b = x * (x + 10) / ( x * x+ 12 );
    auto c = (x ^ 4 + x ^ 3 + x ^ 2 + x + 100 ) / ( x ^ 2 + 10 );
    

    The type of such expressions would be even more huge and ugly, but thanks to auto, we now can let the compiler infer the type of the expressions.


    So the bottomline is: the keyword auto might increase or decrease clarity and readability of your code, depending on the context. If the context makes it clear what type it is, or at least how it should be used (in case of standard container iterator) or the knowledge of the actual type is not even needed (such as in expression templates), then auto should be used, and if the context doesn’t make it clear and isn’t very common (such as the second case above), then it should better be avoided.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Python nested functions variable scoping After much trial and error I have
Possible Duplicate: Auto-size dynamic text to fill fixed size container. Let's say i have
Possible Duplicate: c++ using too much cpu my game uses over 50% of cpu.
Possible Duplicate: ASP.NET “special” tags I hope this isn't too much of a newbie
Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have
Possible Duplicate: How many lines of code should a function/procedure/method have? I would like
Possible Duplicate: Workaround for basic syntax not being parsed I am trying to allow
Possible Duplicate: How much does it cost to develop an iphone application? Hope somebody
Possible Duplicate: Why do people use __(double underscore) so much in C++ I was
Possible Duplicate: Compare 2 dates with JavaScript I haven't done much JavaScript. I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.