3 ms·
And just a few lines below that: "for (auto item : { 1, 2, 3, 4, 5, 6, 7, 8, 9 })". Why?? It's an almost comical abuse of C++11 for the sake of it.
by andrepd 11y ago
And just a few lines below that: "for (auto item : { 1, 2, 3, 4, 5, 6, 7, 8, 9 })". Why?? It's an almost comical abuse of C++11 for the sake of it.
- nadams 11y ago> And just a few lines below that: "for (auto item : { 1, 2, 3, 4, 5, 6, 7, 8, 9 })". Why?? It's an almost comical abuse of C++11 for the sake of it. This comment is very interesting. His syntax reminds me of Python syntax of: for i in [1,2,3]: Isn't that what the future C++ is trying to achieve? Simplified syntax?
- detrino 11y agoIt's worse in every way to: for (int i = 1; i != 10; ++i)
- srean 11y ago> Isn't that what the future C++ is trying to achieve? Simplified syntax? I think your question was rhetorical, in any case the answer is yes. In fact the proposed C++ pythy syntax tries go even further http://pfultz2.github.io/Pythy/ http://pfultz2.github.io/Pythy/ . IIRC its Clang only. Its author hangs out on HN sometimes and would be able to provide more information
- andrepd 11y agoEven in that example, wouldn't it be better to simply write for i in range(1,4):
- sjolsen 11y agoWhy? Because that's the most concise way in general to iterate over an arbitrary list of enumerated values. There's no "abuse" here, just a particular example that could have been written more concisely.
- fauigerzigerk 11y agoIt's not the most concise way to iterate over a sequence of numbers from 1 to 9. Worse than that, it suggests that there may be a number missing somewhere in the middle. You have to scan the entire sequence with your eyes to be sure. for (int i = 1; i <= 9; ++i) is both more concise and requires less thinking to know what's going on.
- sjolsen 11y agoThat the data he chose can be expressed as a range is utterly inconsequential. The values are never even used. In all likelihood, the author just wrote for (auto item : {}) and then filled in the first sequence of numbers that popped into his head.
- fauigerzigerk 11y ago>That the data he chose can be expressed as a range is utterly inconsequential It could be, but we don't know that. And that's exactly my point. The syntax he uses forces us to think and even speculate about that. So for the particular sequence he chose for his example, it would be less ambiguous to generate the range algorithmically.
- emmelaich 11y agoThe point of using this sort of syntax for scrapcode is that you can just textually replace the the {...} with a real container variable. If you should be so inclined later.