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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:31:17+00:00 2026-06-17T16:31:17+00:00

It would seem that the for each style of syntax available in C++11 permits

  • 0

It would seem that the “for each” style of syntax available in C++11 permits array iteration without knowledge of the actual size of the array (number of elements). I assume, since it is part of the new standard, that this is perfectly safe, even for C arrays. Typically, you must also separately know the size of a C array before manipulating it, but I want verification from anyone experienced with this new C++ technique that it works exactly as you’d expect:

extern float bunch[100];

for (float &f : bunch) {
  f += someNumber;
}

Is there anything I should know about non-obvious side effects or disadvantages to this technique? It doesn’t show much in code I see, probably because most of the code was written before this was in the standard. Want to make sure its rare usage isn’t because of some other reason not well-known.

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

    There is nothing strange or unsafe about that usage. The array’s size is known at compile time, so it can be used in the loop. This is an example of a template function that allows you to find out the length of an array:

    template< class T, std::size_t N >
    std::size_t length( const T (&)[N] )
    {
      return N;
    }
    
    Foo f[5];
    std::cout << length(f) << "\n";
    

    This should make it clear that you cannot use this technique, or range based loops, on dynamically sized C-style arrays.

    Of course, if you have range based loops then you should also have std::array (if not, you can probably get ti from std::tr1 or boost), so you can avoid the C-style array entirely:

    extern std::array<float, 100> bunch;
    
    for (auto &f : bunch) {
      f += someNumber;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It would seem that IE7 puts an extra 1px of spacing above and beneath
I can't seem to find anything that would let me to easily show a
The well-cited RIP Hash rocket post would seem to imply the Hash Rocket syntax
I'm trying to apply unknown styles to unknown selectors and it would seem that
I'm currently building a matrix style report. After each rowgroup, i would like to
This would seem to be the case in Firefox 3.5+, there I can instantiate
What are the best use cases for anonymous types? It would seem to me
Hi I seem to be at an ORM Tool crossroad and would like some
It doesn't seem like it would be too hard to implement in assembly. gcc
They both seem exceedingly similar and I'm curious as to which package would be

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.