C abuse of the week
A while back I wanted to write some C code that looped over every element in a collection. But I wanted the code to be parametrisable in the face of different implementations of that collection, because it might have to link against several other pieces of code which might all store the things in question in a different format.
LOOP_OVER_ALL_ELEPHANTS(elephant) {
/* loop body executed once for each elephant */
}
And then each implementation would define the macro LOOP_OVER_ALL_ELEPHANTS appropriately for how it happened to be storing its elephants. If it was an array, the macro would expand to
for (elephant = array; elephant < array + len; elephant++)
and if it was a linked list, it would expand to
for (elephant = head; elephant != NULL; elephant = elephant->next)
and I presumed that any more complicated thing like a C++ STL collection would have some kind of similar idiom available.
Unfortunately, the very first implementation I tried to test against turned out to be storing its elephants in a circularly linked list –
for (elephant = head; elephant != head; elephant = elephant->next)
then the loop terminates after zero iterations and you look foolish.
I solved the problem by rewriting my loop in the form
LOOP_OVER_ALL_ELEPHANTS(elephant) {
/* loop body executed once for each elephant */
} END_LOOP_OVER_ALL_ELEPHANTS(elephant)
which permitted the porting code to define a pair of macros containing the two ends of a do-
But this morning, I do believe I've solved it. You can iterate over a circularly linked list using a construction that can be encapsulated into my original macro, requiring no loop machinery after the single closing brace. It's strictly legal standard C as far as I can see, and it goes like this:
switch (elephant = head, 0)
while (elephant = elephant->next, elephant != head)
case 0:
Then you can follow that with a single braced block (or even an unbraced statement), and everything works –continue
. So I could have put that lot into my macro, and it would have been fine!
More generally, this construction permits a sort of variant form of the for
statement, which does its test after the loop body rather than before. You could almost define it as a macro in its own right:
#define for_after(init, condition, increment) \
switch(init, 0) while (increment, condition) case 0:
Of course that isn't quite syntactically right, because the three clauses of the for
are separated by commas rather than semicolons, and can't contain initialisers in the way that a proper for
can. Also there's the unusual constraint that the body of such a statement cannot contain case statements intended to bind to switches outside the loop (so you can't combine a loop of this form with Duff's Device).
But it's closer than I thought I'd be able to get!
eta: there's a bug in the switch
-based solution above. See my follow-up post for details.
no subject
no subject
Unfortunately, one of the wrinkles is the key distinction between an input iterator and a forward iterator: you can copy a forward iterator and use the copy later to make a second pass through the data, whereas an input iterator is once-only (compare traversing a linked list with reading lines from a TCP socket). This distinction between contexts can't be directly enforced by the compiler. There is an iterator_category mechanism to get around the problem, but it's clunky.
There is also a proposal to extend the language with explicit concepts, but it's typical Stroustrup filth and was rightly rejected from C++0x. In due course, someone will come up with something nice to replace it, I hope.