Commas are less scary than people are taught to believe.
And I wrote the comma so I don't have to look at dumb “zip” fluff — extra verbiage to make your code look... like the obvious?
|
for (auto [x, y] : zip( xs, ys )) // how is this more readable?
|
Zip is stupid anyway.
|
for (auto [x, y, z] : zip( xs, ys, zs )) // foo, won't compile!
|
Betcha the C++ Committee will totally miss that use case. Every extant zip I’ve seen, including the ‘good’ ones *ahem*boost*ahem* have the same limitation.
What if the ranges are unequal in length? Every extant zip implementation barfs.
My fancy comma doesn’t.
1 2 3 4 5 6
|
std::string cs = "ABCD"; // Hey, aren't there too many letters here?
int xs[] = { 1, 2, 3 };
std::vector <double> ds { .1, .2, .3 };
for (auto [c, x, d] : cs, xs, ds) // Let's find out:
std::cout << c << "," << x << "," << d << "\n";
|
Prints:
Huh.
No failure. Seems it worked as intended.
I too have always read the dire warnings about messing with the comma operator, and seen contrived examples of abuse to demonstrate problems. But honestly, every time I have played with RLM it has been pretty clear that you would be hard-pressed to break correct code with it.
Sure, I suppose it could break some stupidly-written code somewhere where someone thought he was being cool by using a comma instead of just writing two statements or some other “don’t do that!” karma problem.
Tough beans on bad code.
In
this case, SFINAE makes it only work over classes where begin() and end() work, and packs it into a tuple of zip_iterator_pairs. If you try to use it outside this context you’ll have to explicitly bracket it anyway, and the tuple type will not likely be compatible with whatever you’re trying to do wrong, so it is pretty obvious when you’re misusing it.
All that for less than 150 lines of
liberally commented code... and most of that was the zip_iterator class boilerplate. Heck, I didn’t even have to torture any meta-templates to do it.
Pretty proud of myself for that one, I am.
Too bad the beautiful C++ trying to get out keeps getting saddled by C++++ holy language stuff. I can’t tell you how much time I wasted trying to use various zip() implementations before my brain popped and I just wrote my own. (That works.)
/end , zip rant
Let’s eat, grandpa!
:O]