473,756 Members | 1,818 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2869
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
2574
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 libs; use /NODEFAULTLIB:library Here is the compiler options: /Od /I "C:\DriverX4\Include" /I "..\ThreadLib" /AI "..\Utility\bin\Debug" /AI "..\Logging\bin\Debug" /AI "Debug" /D "WIN32" /D "_DEBUG" /D "_WINDLL"
3
7221
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 'mscorlib' (Signature='B77A5C561934E089' Version='1.0.5000.0') of assembly 'System.dll' WARNING: Unable to find dependency 'mscorlib' (Signature='B77A5C561934E089' Version='1.0.5000.0') of assembly 'System.Windows.Forms.dll' WARNING: Unable to...
1
2003
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: curses.h: check for missing prerequisite headers? configure: WARNING: curses.h: see the Autoconf documentation configure: WARNING: curses.h: section "Present But Cannot Be Compiled" configure: WARNING: curses.h: proceeding with the...
92
6239
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
2154
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; } By definition, readonly fields can only be initialized inside a
0
9275
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10034
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9843
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9713
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7248
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3805
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3358
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.