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: https://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: https://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.