Can I make GCC warn on passing too-wide types to functions?

流过昼夜 提交于 2019-11-27 05:25:42

Use -Wconversion -- the problem is an implicit cast (conversion) from long x to short when the function f(short x) is called [not printf], and -Wconversion will say something like "cast from long to short may alter value".

..

Edit: just saw your note. -Wconversion results in a warning for me, using g++ 4.3.2 on Linux... (4.3.2-1 on Debian)

I would strongly suggest investing in PC-lint/FlexeLint from Gimpel. The software is made for catching things like this that the compiler just isn't. It is relatively inexpensive and well worth the price. There is an online demo on the site that you can use to evaluate it, here is what it reports for the line in question from your example:

diy.cpp  14  Info 734: Loss of precision (arg. no. 1) (31 bits to 15 bits)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!