Jump to content
  • Advertisement
Sign in to follow this  
Jacob Jingle

conversion from x to y = loss of data

This topic is 2605 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

C++ 32/64-bit MSVC: I'm getting C4244 warnings:
conversion from 'INT' to 'FLOAT', possible loss of data
conversion from 'FLOAT' to 'LONG', possible loss of data
conversion from 'size_t' to 'UINT', possible loss of data
And vice versa.

What's the best way to deal with these?

Thx ahead of time.

Share this post


Link to post
Share on other sites
Advertisement
What are you trying to accomplish doing these conversions? Maybe you could've avoided truncating the variables entirely in the first place?
Otherwise, split them up into multiple variables with lower precision.

It's really better if you post an example, so the guys can advice based on the exact scenario that you experience now.
First think about why you need to cast those values. And remember: T static_cast<T>(expr) is your friend if you need to cast stuff! :)

Share this post


Link to post
Share on other sites

C++ 32/64-bit MSVC: I'm getting C4244 warnings:
conversion from 'INT' to 'FLOAT', possible loss of data
conversion from 'FLOAT' to 'LONG', possible loss of data
conversion from 'size_t' to 'UINT', possible loss of data
And vice versa.

What's the best way to deal with these?

By looking at the code that generates them and seeing how you can avoid making those conversions. Or where satisfied that the possible loss of data is precisely the behaviour you want, then as a last resort use a static_cast.

Share this post


Link to post
Share on other sites

What are you trying to accomplish doing these conversions?

For everything but the size_t warnings, they are API function return values being used as arguments for other API functions.
For the size_t warnings, I get it for using vector/string's .size() as input into a API functions.
ID2D1HwndRenderTarget->DrawText(wstring.c_str(), wstring.size(), ...etc...);

Share this post


Link to post
Share on other sites

[quote name='SuperVGA' timestamp='1304363721' post='4805607']
What are you trying to accomplish doing these conversions?

For everything but the size_t warnings, they are API function return values being used as arguments for other API functions.
For the size_t warnings, I get it for using vector/string's .size() as input into a API functions.
ID2D1HwndRenderTarget->DrawText(wstring.c_str(), wstring.size(), ...etc...);
[/quote]
I'm not sure which DrawText() function that is, but you might check and see if a second argument of '-1' is taken to mean 'treat the input string as null-terminated'. If so, you could just submit -1 and avoid the problem entirely.

Otherwise, assuming you know what you're doing, you can use static_cast<>(), as mentioned previously. The case of needing to submit an unsigned integer value to an API that expects a signed integer is fairly common, I think, and a static_cast should be fine in that case. (I'd be more wary of casting a floating-point type to an integer type though. There are certainly cases where that's appropriate, but just be sure you have a good reason for doing it.)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!