#include <iostream>
using namespace std;
template<typename Iterator, typename Fn1, typename Fn2>
void for_the_device(Iterator from, Iterator to, Fn1 always, Fn2 butFirst) {
switch ((from == to) ? 1 : 2) {
case 0:
do {
butFirst(*from);
case 2:
always(*from); ++from;
} while (from != to);
default: // reached directly when from == to
break;
}
}
int main() {
int const items[] = {21, 42, 63};
int const * const end = items + sizeof(items) / sizeof(items[0]);
for_the_device(items, end,
[](auto const & i) { cout << i;},
[](auto const & i) { cout << ", ";});
cout << endl << "I'm (still) so sorry" << endl;
// Now on an empty range
for_the_device(end, end,
[](auto const & i) { cout << i;},
[](auto const & i) { cout << ", ";});
cout << "Incredibly sorry." << endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKdGVtcGxhdGU8dHlwZW5hbWUgSXRlcmF0b3IsIHR5cGVuYW1lIEZuMSwgdHlwZW5hbWUgRm4yPgp2b2lkIGZvcl90aGVfZGV2aWNlKEl0ZXJhdG9yIGZyb20sIEl0ZXJhdG9yIHRvLCBGbjEgYWx3YXlzLCBGbjIgYnV0Rmlyc3QpIHsKICBzd2l0Y2ggKChmcm9tID09IHRvKSA/IDEgOiAyKSB7CiAgCWNhc2UgMDoKICAJICBkbyB7CiAgCSAgCWJ1dEZpcnN0KCpmcm9tKTsKICAJY2FzZSAyOgogIAkgICAgYWx3YXlzKCpmcm9tKTsgKytmcm9tOwogIAkgIH0gd2hpbGUgKGZyb20gIT0gdG8pOwogIAlkZWZhdWx0OiAvLyByZWFjaGVkIGRpcmVjdGx5IHdoZW4gZnJvbSA9PSB0bwogIAkgIGJyZWFrOwogIH0KfQoKaW50IG1haW4oKSB7CiAgaW50IGNvbnN0IGl0ZW1zW10gPSB7MjEsIDQyLCA2M307CiAgaW50IGNvbnN0ICogY29uc3QgZW5kID0gaXRlbXMgKyBzaXplb2YoaXRlbXMpIC8gc2l6ZW9mKGl0ZW1zWzBdKTsKICBmb3JfdGhlX2RldmljZShpdGV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