Why not a warning?

B

BSeab1024SE

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.)

Also, does the C++ standard dictate the types of statements that
should generate warnings or is that left to the compiler author?

Regards,
Brian
 
K

Kai-Uwe Bux

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
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

In MS Visual C++ some code I wrote generated the warning "conversion
from 'int' to 'const float', possible loss of data."

Since your question has already been answered I'd like to point out
that the easiest way to remove the warning is to use a double if
possible. On most platforms a double can give an exact representation
of all values an int can contain.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top