[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Coding style
From: |
Hans Aberg |
Subject: |
Re: Coding style |
Date: |
Thu, 13 Jan 2011 19:15:35 +0100 |
On 13 Jan 2011, at 16:50, David Kastrup wrote:
Doesn't C++ have some more useful
idiom for iterators or so?
Then you'd have to write a function to apply. Below is an example; I
let you decide if it is more useful. :-)
Something like
for (d=UP;;d=DOWN) {
...
if (d == DOWN)
break;
}
would be somewhat prettier in my book and involve less calculation.
Or
for (d=UP;d==UP;d=DOWN) {
...
}
or
#define UPDOWN(x) for ((d)=UP;(d)==UP;(d)=DOWN)
UPDOWN(d) {
...
}
But macros and templates tend to produce nasty errors.
---- up_down.cc ----
#include <iostream>
#include <algorithm>
struct up_down {
enum token { UP, DOWN, END } _value;
up_down() : _value(UP) {}
up_down(token x) : _value(x) {}
token operator*() { return _value; }
token operator++() {
switch (_value) {
case UP: _value = DOWN; break;
default: _value = END;
};
return _value;
}
friend bool operator==(const up_down& x, const up_down& y) { return
x._value == y._value; }
friend bool operator!=(const up_down& x, const up_down& y) { return
x._value != y._value; }
static up_down begin() { return UP; }
static up_down end() { return END; }
};
void f(up_down::token x) {
switch (x) {
case up_down::UP: std::cout << "Hello "; return;
case up_down::DOWN: std::cout << "World!"; return;
};
}
int main() {
std::for_each(up_down::begin(), up_down::end(), f);
std::cout << std::endl;
return EXIT_SUCCESS;
}
----------------