如何处理遗留代码中的方法名称重复
how to deal with method name duplication in legacy code
我的遗留代码中有这个
#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>
并抱怨它们愚蠢的min
和max
宏时,这总是让我很困扰)。
也就是说,一个快速而肮脏的解决方案可能是用代替宏定义
#include <algorithm>
using std::min;
using std::max;
尽管如此,这仍然污染了全局名称空间,这些名称空间现在是合适的函数名称,可以被任何局部变量或任何其他函数或方法隐藏,而不仅仅是到处被愚蠢的文本替代预处理器取代。
除此之外,请考虑在这些宏(或using
)之前包含任何系统包含文件。
相关文章:
- 使用不同的CRT将新的C++代码与旧的(二进制)组件隔离开来的最佳方法是什么
- 当无法使用模板和宏时,生成类型变体C++代码的最简单方法是什么?
- 有没有一种代码密度较低的方法来使用非默认构造函数初始化数组?
- 如何重写全局方法名称以在调用原始方法之前将我的代码推到前面
- 在nodejs中使用本机代码的最佳方法是什么?
- 函数从唯一代码调用正确的子类方法
- 如何启用使用另一个类的静态方法的模板函数的自动推导,从而消除冗长的调用方代码
- 有没有更好的方法来编写这个c++代码
- 终端进程终止,退出代码为:1有什么方法可以修复它吗
- 有没有一种预处理器的方法可以从调试符号中删除代码段
- C++:销毁此代码的最佳方法是什么?
- 在.cpp文件中定义方法而不是在 C++ 的 .h 文件中定义方法时,如何提高代码的性能?
- 为什么在我的代码中没有调用 g 模拟方法
- 设计许多单例代码结构的更好方法
- 两种专用方法中的相同代码
- Android 无法从本机代码调用 Java 方法 JNI
- 我的类中有方法的指针数组,但我不能调用我的方法.代码如下
- 如何在子类中获得相同的方法代码而不重复代码
- 如何只执行一次方法代码,尽管每帧调用一次
- 模板化的方法代码在哪里分配?