4 ms·
Could you explain why in this hypothetical scenario a warning should be emitted for “for (int i = 0...)”?
by 0db532a0 8y ago
Could you explain why in this hypothetical scenario a warning should be emitted for “for (int i = 0...)”?
- inetknght 8y agoRange-based for loops are far more idiomatic for modern C++: https://stackoverflow.com/a/15089298/1111557 https://stackoverflow.com/a/15089298/1111557
- saagarjha 8y agoThey're also not in the standard library. Not everyone can justify integrating Boost into their applications :(
- Y_Y 8y agoYou can certainly do this with just the standard library and no boost, for example using iota from <numeric>. https://en.cppreference.com/w/cpp/algorithm/iota https://en.cppreference.com/w/cpp/algorithm/iota
- saagarjha 8y agoThat’s not a generator, though. You need to allocate an extra container for this to fill, which is inconvenient.
- bonzini 8y agoAnd underneath they compile to an int for loop and a pointer dereference, both of which can be undefined behavior.
- platinumrad 8y agobonzini's hypothetical scenario was one where the compiler emitted a warning every single time it encountered undefined behavior. Assuming that a loop counter cannot overflow because signed integer overflow is undefined is one such instance and would fall under the "99% false positives" that they mention.