2

I would like to be warned about implicit conversions from any floating point type to any integer type, but not about the narrowing conversion from double to float. E.g for this code

int main() {
    float f = 0.2f;
    int i = 1;
    double d = 0.3;
    i = f;
    i = d;
    f = i;
    f = d;
    d = i;
    d = f;
}

I'd like to get a warning on i = f; and i = d;, but not on any other line.

In clang, this can be achieved via -Wfloat-conversion or -Wconversion -Wno-implicit-float-conversion (the latter one will enable all conversion warnings except that single one I don't want). But in GCC, things are different. -Wfloat-conversion enables all lossy conversions from floating point types at once and I cannot find a flag to further differentiate what it is being converted to. -Wimplicit-float-conversion does not exist.

1
  • Coincidence, I tried a very recently to do the same. I didn't know there was a way with Clang, I will give it a try. I tried with C4244 of MSVC and a script to filter out floating to integer conversions only. My motivation: conversions of float to integer is UB in case of overflow instead of just wrapping out I was expecting for years. Commented Sep 27, 2024 at 19:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.