为什么定义的“最大”宏象在C?
#define max(a,b) \
({ typeof (a) _a = (a); \
typeof (b) _b = (b); \
_a > _b ? _a : _b; })
为什么不直接(A> B A:B)?
由于otherwhise MAX(F(1),F(2))
将调用两个中的一个功能两次:
because otherwhise max(f(1), f(2))
would call one of the two functions twice:
f(1) > f(2) ? f(1) : f(2)
而不是由高速缓存的两个值 _a
和 _B
你
({
sometype _a = (a);
sometype _b = (b);
_a > _b ? _a : _b;
})
(并明确其他人指出,有一个与自动增量/自动递减同样的问题)
(and clearly as other have pointed out, there is the same problem with autoincrement/autodecrement)
我不认为这是由Visual Studio这样的支持。这是一个复合语句。读到这里确实有MSVC gcc的模拟({})
I don't think this is supported by Visual Studio in this way. This is a compound statement. Read here does msvc have analog of gcc's ({ })
我要补充一点,这里给出在GCC手册中的复合语句定义的http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_4.html#SEC62显示了code非常相似,最大的问题之一: - )
I'll add that the definition of compound statement in the gcc manual given here http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_4.html#SEC62 shows a code VERY similar to the one of the question for max :-)