Following is some obviously-defective code for which I think the compiler should emit a diagnostic. But neither gcc
nor g++
does, even with all th
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)