Is there a shortcut for this "pattern": c = (a -b);
if (c != 0) return c;
c = (d-c);
if (c != 0); return c;
//...
I don't quite see how I can avoid storing c, because it matters if it's <0 or >0... or does it get optimized away anyway?
--
qsort, bsearch, and so on take a function whose arguments need to be "const void *p". Is there a way to cast them into the right type right in the function definition, without needing to either introduce two new variables, or cast them every time they're used?
-1, 0 or +1 is fine though. I don't need the exact value. If that helps in any way.
Name:
Anonymous2009-10-20 17:25
If you write something like if (a - b) return a - b; the compiler will evaluate the common subexpression a - b only once. But you're worrying about the performance of inconsequential things.
Also note that in your example the second if will be executed only if a - b == 0, which means the second if essentially becomes if (d).
Yuck, the example is messed up then. Obviously I'm not comparing "c". Sorry about that.
I wasn't worrying so much about performance, just that it looks kind of ugly, and needs an extra variable. So I can just make a macro for that expression, and should be done with it. Fine, thanks!
Shut your face you dickhead. I was talking about "pattern" in the sense of a repeated "pattern" of expressions. I know I can write it that way, and guess, it's exactly the same. So shut your face. I'm looking for an easier way to write it, so it's easier to read. Fucking hell you're a shithead.
I'm just looking for a better way to write these expressions, if you have a bunch of them, and the things you're comparing are "long" expressions themselves, function calls, and so on.
Name:
Anonymous2009-10-20 18:29
c new fags itt
Name:
Anonymous2009-10-20 18:54
>>7
If the expression has side-effects then obviously it has to be evaluated every time it occurs, but then that's not what you fucking put in your fucking example now is it?
A compiler that does whole-program optimization could still deduce that the value of the function only depends on the parameters and that it has no side-effects in which case it would only have to be called once AFAIK. I have no idea how common this type of optimization is.
Name:
Anonymous2009-10-20 18:59
the compiler i wrote when i was twelve would only evaluate this once
Name:
Anonymous2009-10-20 19:43
This is where we see the elegance of Perl's a || b || c idiom. Too bad C forces logic results to 0 or 1.
Though it strikes me that I'd probably write the first example as return a !=b?a-b:c != d?c- d: //...
Does it still get optimized the same, I wonder? *lazy*
For the second, you can cast the function pointer to void* when you pass it to bypass the type checking.