BS*********@aol.com wrote:
In MS Visual C++ some code I wrote generated the warning "conversion
from 'int' to 'const float', possible loss of data."
In the process of trying to determine if I could rewrite the code to
eliminate the warning I noticed that the following code did not
generate any warning...
int x = 1024;
char y = x;
The Comeau online compiler generated no warning either.
I thought this would generate a warning also given the narrower width
of the char and the fact that the value of x will not be preserved in
y.
It is not terribly relevant to what I am doing but I am looking to
gain insight.
Any thoughts on why the above code generated no warning (I really hope
I am not missing something obvious.)
FWIW:
int main ( void ) {
int const x = 1024;
char y = x;
}
triggered warnings on Comeau:
"ComeauTest.c", line 3: warning: integer conversion resulted in truncation
char y = x;
^
"ComeauTest.c", line 3: warning: variable "y" was declared but never
referenced
char y = x;
^
It seems that the compiler does not trace the values of variables not
declared const.
Also, does the C++ standard dictate the types of statements that
should generate warnings or is that left to the compiler author?
The standard does not distinguish warnings or errors. It only talks about
whether a "diagnostic message" is required or not. A compiler vendor is
free to add any messages on top of what the standard requires as long as a
compliant program compiles regardless of the additional diagnostics. In
fact, the compiler is even entitled to compile and execute invalid programs
(as per extension). All that is required for a compiler meeting an invalid
program is to issue at least one diagnostic message (e.g, "hello user: your
program has a bug somewhere. have a nice day."). So, compiler writers enjoy
a considerable amount of freedom.
As a matter of quality of implementation, one comes to expect that required
diagonstics are flagged (e.g., as errors) and distinguished from additional
diagnostics that may be issued during the compilation of valid programs
(those diagnostics would be warnings). One also expects that error message
are somewhat helpful in spotting the error. This, however, is not mandated
by the standard.
Details are found in the standard, clause [1.4].
Best
Kai-Uwe Bux