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


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.

Comments (2)

  1. Tom Kirby-Green says:

    Are you sure range based for loops were in Visual Studio 2010? I thought they came in with Visual Studio 2012?

  2. Surf4Fun says:

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

    Thank you for pointing out my error.

Skip to main content