3

我在我的遗留代码中有这个

#define max(x, y)   (x > y ? x : y)
#define min(x, y)   (x < y ? x : y)

那个bean在应用程序中使用了allot,现在我尝试在freeBSD中编译它,我不断得到:

/usr/include/c++/4.2/bits/istream.tcc:123:35: error: macro "min" requires 2 arguments, but only 1 given
/usr/include/c++/4.2/bits/istream.tcc:124:45: error: macro "max" requires 2 arguments, but only 1 given
/usr/include/c++/4.2/bits/istream.tcc:143:33: error: macro "min" requires 2 arguments, but only 1 given
/usr/include/c++/4.2/bits/istream.tcc:144:43: error: macro "max" requires 2 arguments, but only 1 given
/usr/include/c++/4.2/bits/istream.tcc:438:48: error: macro "max" requires 2 arguments, but only 1 given
/usr/include/c++/4.2/bits/istream.tcc:441:53: error: macro "min" requires 2 arguments, but only 1 given
/usr/include/c++/4.2/bits/istream.tcc:449:47: error: macro "max" requires 2 arguments, but only 1 given
/usr/include/c++/4.2/bits/istream.tcc:489:48: error: macro "max" requires 2 arguments, but only 1 given
/usr/include/c++/4.2/bits/istream.tcc:493:53: error: macro "min" requires 2 arguments, but only 1 given
/usr/include/c++/4.2/bits/istream.tcc:501:47: error: macro "max" requires 2 arguments, but only 1 given
/usr/include/c++/4.2/bits/istream.tcc:507:53: error: macro "max" requires 2 arguments, but only 1 given
/usr/include/c++/4.2/bits/istream.tcc:806:43: error: macro "max" requires 2 arguments, but only 1 given

我猜它是代码中方法的名称(宏)。
现在更改名称它的工作量很大。
我怎样才能继续使用它但避免编译器混淆?

4

1 回答 1

4

首先定义这些宏的原因是什么?它是 C++,不需要任何宏,尤其是那些已经被标准作为函数提供给您的宏(当包含<windows.h>并抱怨他们的愚蠢minmax宏时,它总是困扰我)。

话虽如此,一个快速而肮脏的解决方案可能是将您的宏定义替换为

#include <algorithm>
using std::min;
using std::max;

尽管如此,这仍然会污染全局名称空间,这些名称现在是正确的函数名称,可以被任何局部变量或任何其他函数或方法隐藏,并且不会到处被愚蠢的文本替换预处理器替换。

除此之外,请考虑在这些宏(或usings)之前包含任何系统包含文件。

于 2012-08-25T15:00:45.217 回答