By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,290 Members | 1,557 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,290 IT Pros & Developers. It's quick & easy.

Why not a warning?

P: n/a
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

Feb 15 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
Feb 15 '07 #2

P: n/a
On Feb 15, 6:05 am, BSeab102...@aol.com wrote:
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.

--
Erik Wikström

Feb 15 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.