为什么将宏参数放在括号中会导致错误

Why putting a macros argument in parentheses leads to an error?

本文关键字:错误 参数 为什么      更新时间:2023-10-16

关于c++中的预处理器指令,我有一个非常有趣的问题。

考虑以下宏及其用法:

#define FUNCTION(a, b) void (a)(int &current, int candidate)
{
if ((current b candidate) == false){      // Marked Line    
current = candidate;
}
}
FUNCTION(minimum, <)
FUNCTION(maximum, >)

我的问题是,为什么用以下代码行更改"标记行"甚至无法编译:

... if ((current (b) candidate) == false) ...

因为'<'是一个二进制运算符,如果两边都没有操作数,则无法对其求值。您可以在没有宏的情况下验证这一点,只需尝试编译以下代码:

bool LessThan( int a, int b )
{
return( a (<) b );
}

至少您应该看到"预期的表达式"或类似的错误。