为什么定义的“最大”宏象在C? [英] Why is the 'max' macro defined like this in C?

查看:101
本文介绍了为什么定义的“最大”宏象在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 :-)

这篇关于为什么定义的“最大”宏象在C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆