C++/11: Home on the Range-based for-loop

C++/11: Home on the Range-based for-loop

Rate This
  • Comments 2

Home, home on the range,Where the for && the loop play;Where seldom is heard a discouraging wordAnd the skies are !cloudy all day.

If you are from outside of the United States, that joke likely didn’t make any sense.  If it didn’t, it is just a sad, sad, joke on the lyrics to an American Folk song which can be found here: http://en.wikipedia.org/wiki/Home_on_the_range. And even if you are from the United States you might not have gotten it, or thought it was in anyway humorous.  But I thought it was funny.

Range-Based For-Loop

See the link: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2930.html for the precision discussion.

Quick example, note that the index of the array is the “range” for the for-loop:

int roll[6] = { 1, 2, 3, 4, 5, 6 };

for (int& x : array) x *= 2;

Interesting right? Oh, that’s right this has been in Visual Studio since Visual Studio 2012  But it caught my eye this morning.

Leave a Comment
  • Please add 4 and 4 and type the answer here:
  • Post
  • Are you sure range based for loops were in Visual Studio 2010? I thought they came in with Visual Studio 2012?

  • Tom, you are correct.  Thx.  Noticed another typo, which I fixed.

    Thank you for pointing out my error.

Page 1 of 1 (2 items)
Generic Content

Legal Note:

  1. The author, Sam Stokes, is an employee of Microsoft
  2. There is no guarantee that anything I write is correct, I do try to make sure that what I write is correct.
  3. Use anything written in this blog at your own risk
  4. Test what I write about before using the information
  • Sometimes posts will disappear because I realized that they really don't fit with the total chaos of what I write about
  • Links are tested and available on date of publication, what others do with their links are out of my control

Restrictions:

  • Whatever I write on this blog is unedited by management or anyone, which should be obvious from the randomness of the blog.  I simply write about things I like.
  • Microsoft does not restrict me in anyway, so I generallyblather about whatever I want to.
  • For personal reasons I will not write about:
    • Company confidential stuff, mainly because Microsoft is more transparent than any company I have ever worked for.
    • Other people unless they give me permission to write about them