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

The Archive Base Latest Questions

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

const auto& would suffice if I want to perform read-only operations. However, I have

  • 0

const auto& would suffice if I want to perform read-only operations. However, I have bumped into

for (auto&& e : v)  // v is non-const

a couple of times recently. This makes me wonder:

Is it possible that in some obscure corner cases there is some performance benefit in using forwarding references, compared to auto& or const auto&?

(shared_ptr is a suspect for obscure corner cases)


Update
Two examples that I found in my favorites:

Any disadvantage of using const reference when iterating over basic types?
Can I easily iterate over the values of a map using a range-based for loop?

Please concentrate on the question: why would I want to use auto&& in range-based for loops?

  • 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:15:59+00:00Added an answer on June 13, 2026 at 9:15 pm

    The only advantage I can see is when the sequence iterator returns a proxy reference and you need to operate on that reference in a non-const way. For example consider:

    #include <vector>
    
    int main()
    {
        std::vector<bool> v(10);
        for (auto& e : v)
            e = true;
    }
    

    This doesn’t compile because rvalue vector<bool>::reference returned from the iterator won’t bind to a non-const lvalue reference. But this will work:

    #include <vector>
    
    int main()
    {
        std::vector<bool> v(10);
        for (auto&& e : v)
            e = true;
    }
    

    All that being said, I wouldn’t code this way unless you knew you needed to satisfy such a use case. I.e. I wouldn’t do this gratuitously because it does cause people to wonder what you’re up to. And if I did do it, it wouldn’t hurt to include a comment as to why:

    #include <vector>
    
    int main()
    {
        std::vector<bool> v(10);
        // using auto&& so that I can handle the rvalue reference
        //   returned for the vector<bool> case
        for (auto&& e : v)
            e = true;
    }
    

    Edit

    This last case of mine should really be a template to make sense. If you know the loop is always handling a proxy reference, then auto would work as well as auto&&. But when the loop was sometimes handling non-proxy references and sometimes proxy-references, then I think auto&& would become the solution of choice.

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

Sidebar

Related Questions

Const functions don't seem to care if you dereference a member-pointer into a non-const
I have a const int that is computed at compile time in my Managed
I would like to perform the same operation on several arrays, something like: #include<vector>
Is there any way to capture by value, and make the captured value non-const?
I find myself using C++11 more and more lately, and where I would have
I have class foo that contains a std::auto_ptr member that I would like to
In C++03 you can use the const& trick (or most important const) to extend
I have a bunch of objects in a class hierarchy and would like to
I need a generic function that can take either a const or non-const reference
I'm using VS2010. In the constructor of my non-compyable Scene class I have :

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.