I'm seeing strange behavior with the MAX macro in Objective C. Specifically, I have this code in my main function:
NSArray* array = [NSArray array];
NSLog(@"[array count] - 1 = %d", [array count] - 1);
NSLog(@"MAX(0, [array count] - 1) = %d", MAX(0, [array count] - 1));
NSLog(@"MAX(0, -1) = %d", MAX(0, -1));
The output is:
[array count] - 1 = -1
MAX(0, [array count] - 1) = -1
MAX(0, -1) = 0
I saved the preprocessor output with -save-temps, and it looks like this:
NSArray* array = [NSArray array];
NSLog(@"[array count] - 1 = %d", [array count] - 1);
NSLog(@"MAX(0, [array count] - 1) = %d", ((0) > ([array count] - 1) ? (0) : ([array count] - 1)));
NSLog(@"MAX(0, -1) = %d", ((0) > (-1) ? (0) : (-1)));
All the necessary parentheses are there, and [array count] - 1
has no side effects, so the usual macro issues shouldn't apply. Any idea what's going on?