I think you are confusing the compiler with a lint tool. The compiler
is only required to report errors, not warnings. The warnings are just
an icing. True, we expect a lot of icing from the compiler, but still
that's all it is. If you need deep code analysis you should use lint.
"Carl Daniel [VC++ MVP]" <cpdaniel_remove_this_and_nospam@mvps.org.nospam>
wrote in message news:uKhQE6hTIHA.1168@TK2MSFTNGP02.phx.gbl...
Alan Carre wrote:
In this case, I think the compiler should be able to determine that
&(s.i) is an address of a local variable and should issue the warning:
warning C4172: returning address of local variable or temporary
I would definitely say that this is a bug in the compiler.
I'd call it the lack of a feature. There's nothing in the standard or
elsewhere that stipulates the constructs for which a compiler should
generate warnings or errors. In this case, the VC++ team appears to have
implemented a warning for the easy case, which probably covers 90% of the
real-world situations.
Failure to generate *some* warnings can very-often be labeled "compiler
bugs". I wouldn't expect the compiler to catch everything but there are
definite cases (such as this one) where an expected warning can and should
be issued.
For instance, here's another one. Even at warning level 4 the following
code does not generate any warnings:
int j=4;
bool b = j = 7 ? true : false;
The programmer's intention is quite clear, he/she meant to test whether or
not j is equal to 7. Instead j gets assigned the value 1 and b becomes
"true".
Of course nobody should ever write code this way (ie. leaving out brackets
in conditional :? expresions), but the compiler can easily bring our
attention to this error by issuing a "warning: conditional expression is
constant". I mean, if I've turn that warning on why doesn't it tell me
when conditional expressions are constant? In a large project one requires
that the compiler do as it's told so we can trust that a "warningless
compile" contains none of the specific expression types we asked the
compiler to alert us to.
Side note: if we always remember to put in brackets when using :? we will
catch this one via. "warning: assignment in conditional"!
I'm not sure if anyone tried, but I'd try compiling this code with /W4
and /O1. Many more warnings are diagnosed in an optimized build (since
they're produced by the optimizer).
I haven't, but I will certainly try it. Good suggestion.
- Alan Carre