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

  • Home
  • SEARCH
  • 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 3223706
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:08:18+00:00 2026-05-17T16:08:18+00:00

In C++11 we can now do : void dosomething( std::vector<Thing>& things ) { for(

  • 0

In C++11 we can now do :

void dosomething( std::vector<Thing>& things )
{
    for( Thing& thing : things )
    {
        dofoo( thing );
        wiizzz( thing );
        tadaa( thing );
    }

}

I know that the addition and use of lambda is syntactic sugar but it provide interesting optimization opportunities.

What about the for loop? Is it only syntactic sugar or can the compiler optimize some cases that it couldn’t or would be too hard to do with handwritten loop?

  • 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-17T16:08:18+00:00Added an answer on May 17, 2026 at 4:08 pm

    It’s just a syntactic sugar since the standard says that it’s equivalent to a loop with iterators [ Edit: this means it doesn’t provide any additional information to the compiler compared to the equivalent for loop — end edit ]. You may get a better performance though since it’s equivalent to:

    for(auto iter = con.begin(), end = con.end(); iter != end; ++iter)
    {
        auto& ref = *iter;
        // ...
    }
    

    while most people may write:

    for(auto iter = con.begin(); iter != con.end(); iter++)
    {
        // use *iter directly
        // ...
    }
    

    which may be slower if the con.end(), iter++ or *iter are not trivial.

    [ Edit:

    lambda is syntactic sugar

    Not really. Unlike for loop it allows the compiler to capture the stack-frame base pointer directly, for variables captured by reference this saves one address indirection per each use, compared to a handcrafted function object. — end edit ]

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

Sidebar

Related Questions

I understand that you can now create MVC-specific user controls, but will my existing
Now that I can make useful user controls in WPF (thanks to this stackoverflow
I read somewhere (can't find it now) that large exception hierarchies are a waste
Background: I have a kubuntu laptop right now that I can't use wirelessly, i.e.
I have a function of the form: void DoSomething(const boost::function<bool ()>& condition, other stuff);
in the System.Linq namespace, we can now extend our IEnumerable's to have the Any()
How do you install Boost on MacOS? Right now I can't find bjam for
For some weeks now I simply can't run gem install in windows. It sticks
Obviously I can do and DateTime.Now.After - DateTime.Now.Before but there must be something more
The best I can come up with for now is this monstrosity: >>> datetime.utcnow()

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.