How can you say this with a straight face? A proper foreach? You call std::for_each a proper foreach?
C# has a proper foreach. Requiring one to provide a function object is not a proper foreach.
Yes, C# has better primitive support for the foreach construct than C++. However, the fact that the C++ foreach must be implemented using a function object does not make it "not a proper" foreach, as this is an implementation issue stemming from the lack of an existing language primitive.
The closest you can get to a proper foreach is using C++0x's lamba syntax:
Leaves a lot to be desired.
I'd like a real foreach in C++, but std::for_each certainly isn't one.
[/quote]
It is a foreach implemented using the language primitives of C++. It is "proper." There are language deficiencies that prevent the syntax from being ideal, but it still achieves the goals of what the foreach construct semantically means.
Using a built-in control structure to iterate over a container and using a higher-order construct that calls a function object on each member of a container are not the same thing.
[/quote]
If by "not the same thing" you mean the code looks different, then I agree. However, one can achieve the exact same effect as the other, and there is a potential semantic difference.
It's absolutely disingenuous to claim that std::for_each is a replacement for the temporary solution of using a for loop. The syntax was not "retrofitted to emulate the foreach construct." Not only does that not make any sense, you can't provide any evidence for it. The iterator syntax is the way it is in order to emulate pointers from C, which already had iterator semantics.
[/quote]
Using a for loop to iterate over the contents of a C++ standard library collection does not imply the same semantics as a foreach construct in other languages. Easily the first difference is that C++ will happily allow you to modify the collection in the body of the loop without batting an eye, leading to a lot of threads posted on GameDev.net. The for loop was not envisioned as an analog to the foreach concept, and thus using a for loop in place of a foreach was simply out of necessity.
Stroustrup himself prescribes a for loop as a "way of traversing a vector." See this powerpoint presentation, page 37.
http://www.stroustru..._containers.ppt
[/quote]
Of course you can, and for most basic case, I would as well. I am not saying it is incorrect, I am saying that it is semantically different than the foreach concept. The whole point that I am trying to make is that there is a distinction between the use of the for loop as a foreach replacement using iterators, and the use of the for loop as a counted loop. The former is simply a language difficiency, and its syntax usage is not necessarily indicative of idiomatic syntax for a numeric based for loop.