5 ms·
Let's bring up some technical details to see whether there is a deliberate thought process behind the evolution of C++. Recent versions of C++ have the auto key
by KKKKkkkk1 10y ago
Let's bring up some technical details to see whether there is a deliberate thought process behind the evolution of C++. Recent versions of C++ have the auto keyword. It's a cool new feature grafted onto C++ from other languages and meant to make the programmer's life easier. It lets you avoid typing things like
for (std::vector<std::pair<int, double>>::const_iterator it = v.begin(); it != v.end(); ++it) {
foo(*it);
}
The above used to be the recommended way to write a loop until C++11, and was supposedly superior to
for (int i = 0; i < v.size(); ++i) {
foo(v[i]);
}
which was considered to be error prone and unsafe. But now things are much nicer and you can write
for (auto x : v) {
foo(x);
}
Looks so much better. Clearly "auto" is a win for C++ programmers. But wait, it turns out that there's actually a bunch of different ways of writing the auto keyword that have subtly different semantics. You can write "auto", but you can also use every possible combination of "auto" with "const", "&" and "&&". So things like "auto&&" or "const auto&" are also perfectly valid and sometimes preferable to simple old "auto".
Let's put aside the fact that you can't explain the difference between "*", "&" and "&&" to a C programmer without them bursting out laughing. Why does auto have six different variants? Isn't this emblematic of how C++ is evolving?
- pjmlp 10y agoAt least C++ offers me tools to avoid leaking resources or corrupting memory, which cannot be said for C. The fact that C11 made the security Annex optional, is a sign how much the C committee values writing safe code.
- stinos 10y agofor (auto x : v) { usually you'd use for(const auto& : v) though
- dTal 10y agoAs a light C programmer, no part of this post does not make me WTF hard. Would I actually be expected to type that cataclysm at the top? What's wrong with the C-style loop? Was 'auto' the best name they could come up with for this feature?
- hawski 10y agoThey have chosen auto, because it was already a reserved keyword, but it was practically useless.