473,323 Members | 1,537 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

Why not a warning?

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
2 2854
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Bill Burris | last post by:
How do I find what is causing this warning from the Linker? If I use /NODEFAULTLIB I get hundreds of undefined symbols. LINK : warning LNK4098: defaultlib 'MSVCRT' conflicts with use of other...
3
by: DJTN | last post by:
I'm getting the following error when I try to compile my setup project in VS 2002. I have re-installed the .net framework 1.1 and it didnt solve the problem. WARNING: Unable to find dependency...
1
by: spanov | last post by:
i've got problem installing python-2.3.5 from sources on FreeBSD 5.3 root@server# ./configure > conf_log configure: WARNING: curses.h: present but cannot be compiled configure: WARNING:...
92
by: Heinrich Pumpernickel | last post by:
what does this warning mean ? #include <stdio.h> int main() { long l = 100; printf("l is %li\n", l * 10L);
4
by: cody | last post by:
It is possible to declare and use/instantiate a class with a uninitialized readonly field without even a compiler warning. Why don't I get warnings? public class Stuff { public readonly int a;...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.