Last active
January 2, 2017 22:59
-
-
Save viktorasm/2a9d11ac5c7a0896d21f0b15296b5471 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// how I'd "fix" https://akrzemi1.wordpress.com/2017/01/02/not-detecting-bugs/ | |
// | |
// * Explicitly state "code assumes input vectors are same length" | |
// * Explicitly request inlining of loop's inner funcion | |
// * real world code will probably not use "v1, v2" naming | |
// * instead of relying "compiler will optimize away helper constructs", | |
// just don't use any. | |
// | |
// in general, it's a similar case, when instead of ... | |
// struct x = {a[++i], b[i]} | |
// | |
// you write | |
// i++; | |
// struct x = {a[i], b[i]} | |
// | |
// .. and skip the guesswork of what the compiler will choose to do. | |
#include <vector> | |
inline void process(const int sourceId, const int destinationId) { | |
} | |
int main() { | |
std::vector<int> sourceIndexes = {1, 2, 3, 4, 0}; | |
std::vector<int> destinationIndexes = {2, 3, 5, 7, 0}; | |
// verify assumptions in debug | |
assert(sourceIndexes.size()==destinationIndexes.size()); | |
{ | |
auto currentSource = sourceIndexes.begin(); | |
const auto end = sourceIndexes.end(); | |
auto currentDestination = destinationIndexes.end(); | |
for (;currentSource!=end;currentSource++, currentDestination++) { | |
process(currentSource,currentDestination); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment