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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:44:36+00:00 2026-06-13T21:44:36+00:00

A range-based for statement is defined in §6.5.4 to be equivalent to: { auto

  • 0

A range-based for statement is defined in §6.5.4 to be equivalent to:

{
  auto && __range = range-init;
  for ( auto __begin = begin-expr,
             __end = end-expr;
        __begin != __end;
        ++__begin ) {
    for-range-declaration = *__begin;
    statement
  }
}

where range-init is defined for the two forms of range-based for as:

for ( for-range-declaration : expression )         =>   ( expression )
for ( for-range-declaration : braced-init-list )   =>   braced-init-list

(the clause further specifies the meaning of the other sub-expressions)

Why is __range given the deduced type auto&&? My understanding of auto&& is that it’s useful for preserving the original valueness (lvalue/rvalue) of an expression by passing it through std::forward. However, __range isn’t passed anywhere through std::forward. It’s only used when getting the range iterators, as one of __range, __range.begin(), or begin(__range).

What’s the benefit here of using the “universal reference” auto&&? Wouldn’t auto& suffice?

Note: As far as I can tell, the proposal doesn’t say anything about the choice of auto&&.

  • 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-06-13T21:44:37+00:00Added an answer on June 13, 2026 at 9:44 pm

    Wouldn’t auto& suffice?

    No, it wouldn’t. It wouldn’t allow the use of an r-value expression that computes a range. auto&& is used because it can bind to an l-value expression or an r-value expression. So you don’t need to stick the range into a variable to make it work.

    Or, to put it another way, this wouldn’t be possible:

    for(const auto &v : std::vector<int>{1, 43, 5, 2, 4})
    {
    }
    

    Wouldn’t const auto& suffice?

    No, it wouldn’t. A const std::vector will only ever return const_iterators to its contents. If you want to do a non-const traversal over the contents, that won’t help.

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

Sidebar

Related Questions

The following range-based for loop works fine in VS2012: int values[] = {1, 2,
In C++11 , there are two loops over all elements (range based for and
I was trying to erase a range of elements from map based on particular
I need to query my DB based on a range of two values which
Is there a way to have a template specialization based on a range of
I'm trying to group a set of data based on the range of an
I need to convert a number to another value based on a range: ie:
Looking at n3092, in §6.5.4 we find the equivalency for a range-based for loop.
I need to use the value of checkboxes for an IF-THEN statement. Based on
The following range-based-for snippet compiles fine in g++ 4.6.1, but not with clang++ version

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.