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 7620725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:00:28+00:00 2026-05-31T04:00:28+00:00

In the latest C++ standard it implies that: for (foo : bar) baz; is

  • 0

In the latest C++ standard it implies that:

for (foo : bar)
    baz;

is equivilant to:

{
    auto && r = bar;
    for ( auto it = r.begin(), end = r.end(); it != end; ++it )
    {
        foo = *it;
        baz;
    }
}

When bar in the above is a function call that returns a collection, eg:

vector<string> boo();

ie

for (auto bo : boo())
    ...

Doesn’t the line become:

auto&& r = boo();
...

And so the temporary return value of boo() is destroyed at the end of the statement auto&& r = boo(), and then r is a hanging reference at the entry of the loop. Is this reasoning correct? If not, why not?

  • 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-31T04:00:30+00:00Added an answer on May 31, 2026 at 4:00 am

    Is this reasoning correct? If not, why not?

    It is correct up until this point:

    And so the temporary return value of boo() is destroyed at the end of the statement “auto&&r=boo()” […]

    Binding a temporary to a reference extends its lifetime to be that of the reference. So the temporary lasts for the whole loop (that’s also why there is an extra set of {} around the whole construct: to correctly limit the lifetime of that temporary).

    This is according to paragraph 5 of §12.2 of the C++ standard:

    The second context is when a reference is bound to a temporary. The
    temporary to which the reference is bound or the temporary that is the
    complete object of a subobject to which the reference is bound
    persists for the lifetime of the reference except:

    [various exceptions that don’t apply here]

    This is an interesting property that allows abusing the ranged-for loop for non-rangey things: http://ideone.com/QAXNf

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

Sidebar

Related Questions

The standard (latest version : WG14/N1256) says (6.2.1 §2) : A function prototype is
What is the latest version of the Boost library that is compatible with Microsoft
Using the latest SDK from Apple, I noticed that while running on the iPhone
Where can I download the source code of the latest or standard library for
What is the correct way, according to the latest C standard, to define functions
I'm getting a very weird issue with standard ASP.NET backend code, and the latest
Does anyone know the ISO standard that covers .NET 3.5? I found ISO/IEC 23270:2006,
I know that compilers use __STDC__ to indicate that a compiler is standard C
We have a site that works fine with any latest browser including ie8. But
I have a string that I'd like to encode into the standard URL format.

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.