10

Possible Duplicate:
g++ -Wall not warning about double-> int cast

Per the question here, direct conversion from double/float to unsigned integer is not portable. I found I had a few cases in my code where this happens and I would like to tell g++ to warn me if this occurs, but I can't find such an option. Does anyone know if there is an option to do this?

Note: I do see -Wconversion, but that also warns about all kinds of other conversions that I don't care about (like converting int to unsigned int, which is portable per the standard).

Edit: Here's a code example for which I would like to see a warning:

double dblNumber = -234;
unsigned long uintNumber = dblNumber;

On one version of g++, this gives me an integer value of 0xFFFFFF16 (which is -234 in 2's complement) . On another it gives me 0. Clearly the code is ambiguous, which is why it is understandably not considered portable.

6
  • @djechlin: "Note: I do see -Wconversion, but that also warns about all kinds of other conversions that I don't care about (like converting int to unsigned int, which is portable per the standard).".
    – Zeta
    Commented Dec 6, 2012 at 20:50
  • I admit I did not see that other question, but it doesn't really address not using -Wconversion. It seems odd that I it doesn't warn about this by default, especially since different versions of g++ react differently causing certain implementations to silently fail.
    – KyleL
    Commented Dec 6, 2012 at 20:53
  • @KdawgUD Can you show an example piece of code? Not sure how exactly you do the conversion. Whenever you cast explicitly from one type to another, for example un_signed_int_value=(unsigned int) double_value; you are telling the compiler I know what I am doing. So I don't see why compiler would warn about such conversions. In fact, the explicit cast would suppress any warnings if there was one. Unless I misunderstood your question, what you are asking is not possible.
    – P.P
    Commented Dec 6, 2012 at 21:01
  • @Griwes That's why I asked OP post some code to see how exactly it's done. Since OP already stated -Wconversion doesn't detect this, I can't think of anything else on how the conversion is done.
    – P.P
    Commented Dec 6, 2012 at 21:14
  • @KingsIndian, oh, sorry, timestamp reading fail.
    – Griwes
    Commented Dec 6, 2012 at 21:20

2 Answers 2

4

I know you said you don't want to use -Wconversion but it warns on the problem you care about, and at least in g++ 4.5 doesn't warn when converting long to unsigned long (for example). For any other cases that it warns where you're performing a legal desired conversion, just cast it. Your future maintainers will thank you greatly for making it explicitly clear that a known conversion is being done rather than guessing whether it's intentional or not from the code context.

1
  • I 100% agree that explicitly conversions are better. I was just hoping to find a way to set up my build system such that if I mistakenly do an implicit double to unsigned conversion it will not slip through the cracks and go unnoticed. For my project, the warnings produced by -Wconversion are too numerous to be useful, unfortunately.
    – KyleL
    Commented Dec 6, 2012 at 21:34
1

When a float to int implicit conversion is done, and the truncated value cannot fit the destination type, then the behaviour is undefined, as your example nicely illustrates it.

If the only tool provided by g++ is the -Wconversion, perhaps you should make your own tool to filter the warnings out to your convenience (I am thinking the usual sed, awk, perl or php, but it depends on the availability of these tools on your system, and maybe you can do something similar directly from your IDE if you use one).

You can also use specific pragma directives of gcc to restrict conversion checks to specific files, or even specific code parts.

Any other programatic method used to pinpoint in your code where problematic conversions happen could certainly be used to insert explicit casts, which is the goal your should be aiming for.

Not the answer you're looking for? Browse other questions tagged or ask your own question.