如何处理遗留代码中的方法名称重复

how to deal with method name duplication in legacy code

本文关键字:方法 代码 何处理 处理      更新时间:2023-10-16

我的遗留代码中有这个

#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

我猜是代码中方法的名称(宏)
现在更改名称需要大量工作
如何在避免编译器混淆的情况下继续使用它?

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

也就是说,一个快速而肮脏的解决方案可能是用代替宏定义

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

尽管如此,这仍然污染了全局名称空间,这些名称空间现在是合适的函数名称,可以被任何局部变量或任何其他函数或方法隐藏,而不仅仅是到处被愚蠢的文本替代预处理器取代。

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