3 ms·
These 2 might not be the best examples, because "auto" and "foreach" are, without doubt, among the important improvements of C++ 2011. There are many other mor
by adrian_b 5y ago
These 2 might not be the best examples, because "auto" and "foreach" are, without doubt, among the important improvements of C++ 2011.
There are many other more obscure new C++ features, whose usefulness is doubtful.
Having to write again the type, when the type is already provided by an initialization value, does not provide any kind of useful redundancy.
The word "auto" is also redundant, but there is no way to eliminate it without breaking the traditional syntax.
"foreach" also provides a long-needed simplification. It makes no sense to waste time with writing iteration limits and also expressions with either pointers or indices for accessing the elements of a data aggregate, when all these are things that the compiler knows better.
When C (1974) introduced its more powerful "for" syntax, instead of the traditional "for" with arithmetic progressions, that was a partial mistake.
The C "for" allows 2 features that were not available with traditional "for", assigning to multiple loop variables (using the comma operator) and other loop variable updating operations besides addition/subtraction, e.g. indirect addressing through pointer links.
Nevertheless, these 2 features are needed infrequently, but the programmer is forced in 99% of the cases, when a simple "for" is needed, to write a lot of redundant information in comparison with the traditional "for" (e.g. "for i in 0 to 100 by 3 do" vs. "for (i = 0; i < 100; i += 3) {" ) (with traditional "for", the most frequent simpler loops can be written in a simplified way, e.g. "for i to 100 do", when the initial value is 0 and the increment is 1, while in the corresponding C "for" you can only replace "+= 3" by "++").
For the 2 special cases, a better syntax could be found, without altering the syntax for the common case. At around the time when C was developed, the concept of iterators was introduced in Alphard and CLU, which is a better solution than the extended syntax of the C "for".
Introducing "foreach" in C++ 2011, has corrected a mistake dating from C 1974, by providing a simple syntax for the simple common case, so that the complex syntax can be used only when really needed.
The correction is not total, because there still are many "for" loops that cannot be written with "foreach" but which C "for" is still an overkill, e.g. the loops that access multiple arrays.
Nevertheless, not using "foreach" nowadays would be a weird choice (notwithstanding the fringe cases with RHS functions returning references to temporaries, which were discussed on HN some time ago).
- chrsig 5y agoIn the average case, it's generally not an issue, but in a game loop, i could see it being a source of issues. It's really easy to make a mistake and accidentally make a copy of a reference, or obfuscate any conversions...which, I can imagine in the game engine world being quite a massive footgun.
- gambiting 5y agoSorry, I should have made it clearer - there is no issue with either one of these. I was trying to point out that in some industries(like video games, at least where I work - big AAA studio with proprietary engines) the adoption of new C++ features is so glacial, that even relatively inoffensive things like auto or foreach are still being frowned upon. I said in another comment that there is a technical reason why foreach loops are not used sometimes - because we make custom containers for everything and sometimes these containers don't work well in foreach loops(or at all) - it's fixable, but it's easier for the engine to just implement a blanket ban. For us, visibility and explicitness of everything you do is absolute king, and anything that hides types or number of iterations in a loop(!!!) is met with suspicion. I don't necessarily agree, but I see a lot of comments in this thread saying "oh C++ is such a difficult language to learn as a newcomer" - I'm like.....no? Just because these new features exist doesn't mean you have to use them.