Warning C4244 'argument': conversion from 'float' to 'ValueType', possible loss of data
I’m getting a bunch of these warnings in my code, and I’m wondering if there is anyway to deal with them other than a #pragma disabled.
This seems to happen everywhere a Template function (I think that’s what they’re called?) that can accept different value types is used. Here’s an example (pseudo-code) that causes this warning:
float globalScalingFactor = 1.5;
auto componentHeight = 25.0f * globalScalingFactor;
auto componentWidth = 100.0f * globalScalingFactor;
auto marginX = 4.0f * globalScalingFactor;
auto marginY = 4.0f * globalScalingFactor;
textButton.setBounds (area.removeFromTop (componentHeight).removeFromRight (componentWidth).reduced (marginX, marginY));
Each of the uses of the float variables in the removeFromTop(), removeFromRight() etc. calls produce a warning: conversion from ‘float’ to ‘ValueType’.
I’m using floats for my GUI dimensions and distances, and I have 100’s of these type of calls/errors in my resized() functions, and I wonder if there’s a simple way to cast this such that the warnings don’t happen - although it would be a pain to have to cast every single use of the variables…
I know I can hide all of these with #pragma warning(disable : 4244), but I’d like to be able to see “real ones” that might be more important.
What exactly is area in your example? If it’s a Rectangle<int> then you should pass ints to its functions.
If you want to work with floats only, then you’ll need to make area of type Rectangle<float>.
As long as you keep mixing float and int, you will frequently get compiler warnings.
BTW, there is a similar clang warning flag that you can use on macOS. If I remember well, it’s -Wfloat-conversion.
do the right thing, aka cast from float to int. either up front when you do the calculations, or in the setBounds() call. Personally, I do all the math in floats, and then cast to int when needed (ie. setBounds().
Thanks, but I don’t want to convert a Rectangle<float> into a Rectangle<int> - I started this thread with a Rectangle<int>. I’m just trying a Rectangle<float> (now) to see if it solves my other issue. I’d like to know at this point, why setBounds() won’t accept a Rectangle<float> when it uses a ValueType for arguments…
yes, setBounds only takes an int Rectangle, which is why I said I do the casting in my code. I want to preserve accuracy, so I do all position and size calculations in float, and then cast when needed in the setBounds call. I don’t use a float Rectangle with toInt conversion, because I still run into the same issue, as some of my values may be int not float. Anyways, the reason you have 100’s of casts to catch up on, is because you weren’t casting as you wrote the code (or at least when the warning came up when compiling). Bite the bullet and do the casting update, then just keep your compiles free of warnings as you work. Rarely do you want to disable a warning with a pragma, most of the time you should do the right thing to fix the warning.
Well, I started working on Mac, where there are no warnings for this kind of thing. Xcode doesn’t care. It’s only when I added Windows to the mix that I noticed this issue. Now I have a zillion of these.
I hear you there. I guess I’ll just have to cast every single variable in my calls to setBounds(). I like the idea of keeping the GUI calculations in the float realm.