More extensions to C
KW Heuer
kwh at bentley.UUCP
Tue May 27 05:34:12 AEST 1986
In article <1483 at mmintl.UUCP> mmintl!franka (Frank Adams) writes:
>In article <826 at bentley.UUCP> kwh at bentley.UUCP writes:
>>[Swap] could be considered a special case of my ",," operator: to exchange
>>x and y, write "x = (y ,, (y = x))".
>
>I would prefer a syntax like "{x, y} = {y, x};" It's more readable.
Yes, for a swap. The real power of ",," is for removing explicit temporary
once-only variables in general; for example, it allows the function
int pop() {
int temp1;
struct stack *temp2;
temp1 = stk->top;
temp2 = stk->next;
free(stk);
stk = temp2;
return (temp1);
}
to be abbreviated to the macro
#define pop() (stk->top,,(stk=(stk->next,,free(stk))))
(A sort of generalization of the postfix operators. "++x" means "x=x+1", but
"x++" means "x,,(x=x+1)".)
>(BTW, a really good optimizing compiler can optimize out the temporary. I'm
>not fully up on what optimizers are doing these days, but I doubt that there
>are many compilers which do this. Optimizing it out is easier if it is
>declared locally ("{int temp = x; x = y; y = temp;}") instead of being
>declared at the top of the routine.)
Unfortunately, I think the "clue" gets lost before the optimizing phase.
Karl W. Z. Heuer (ihnp4!bentley!kwh), The Walking Lint
More information about the Comp.lang.c
mailing list