C ++如何防止我的团队开发人员使用abs的整数版本错误? [英] C++ How can I prevent my team developers from using integer version of abs by mistake?
问题描述
我的团队正在编写代码来为两个Windows(使用 VS2015
)和Android(使用 GCC 4.9
调用 QtCreator
)。
我们发现Android二进制文件有 abs 函数。
double a = 1.0;
double b = 0.5;
std :: cout<< abs(a-b)< std :: endl;
std :: cout<< std :: abs(a-b)<< std :: endl;
显示:
code> 1
0.5
这是一个已知问题,其他):
使用abs时的奇怪错误()我最近遇到了
有很多地方我们使用 abs
将它们全部替换为 std :: abs
。精细。 但是,如何防止 abs
以后再次使用?
找到本主题:避免使用abs()的编译器问题,但它没有帮助。 / p>
我无法启用将所有警告视为错误(-Werror -Wall),因为g ++比MSVC容许的多。即使我们努力在MSVC上编写0警告,我们仍然得到吨的g ++(其中可能有一个关于abs使用不当),我们历史上忽略它们。修复它们将花费我们太多的精力。
你有一个头文件基本上包括在内吗?某种持有人的基本面?如果是,你可以把它放在这里:
extern void NeverDefined();
inline int abs(int a){
NeverDefined();
return a;
} // abs(int)
c $ c> NeverDefined ,你会知道有问题!
通常我不会定义有问题的函数,
编辑
事实上:不要打扰与头文件(标准定义就足够了)。用上面的代码(无 inline
)编写自己的 abs.cc
,它将取代库定义。 p>
当然,如果原始文件标记函数 inline
,这将无法工作 - 在这种情况下,原定义...
My team is writting code to be compiled for both Windows (using VS2015
) and Android (using GCC 4.9
invoked by QtCreator
).
We figured out that Android binaries had a problem with abs
function.
double a = 1.0;
double b = 0.5;
std::cout << abs( a - b ) << std::endl;
std::cout << std::abs( a - b ) << std::endl;
Displays:
1
0.5
This is a known issue, found this topic (among others): Strange bug in usage of abs() I encountered recently
There are lots of places where we use abs
, I'll replace them all by std::abs
. Fine. But how can I prevent abs
to be used again in the future?
Found this topic: Avoiding compiler issues with abs(), but it did not help.
I can't enable treating all warnings as errors (-Werror -Wall) because g++ is much less permissive than MSVC. Even if we make the effort to compile with 0 warning on MSVC, we still get tons of them with g++ (among them there could be one about abs being used badly) and we historically ignore them. Fixing them all would take us too much effort.
Do you have a header file that is basically included by everything? Some kind of holder-of-all-fundamentals? If so, you can put this in there:
extern void NeverDefined();
inline int abs(int a) {
NeverDefined();
return a;
} // abs(int)
If you then get a linker error on NeverDefined
, you'll know there's a problem!
Normally I simply wouldn't define the function in question, but since it's a library function I had to have this second level.
Edit
In fact: don't bother with a header file (the standard definition will suffice). Write your own abs.cc
with the above code (no inline
) and it will supersede the library definition.
Of course, this won't work if the original file marks the function inline
- in which case you could always edit the original definition...
这篇关于C ++如何防止我的团队开发人员使用abs的整数版本错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!