I have to convert a very large C++ legacy code base to 64 bits. I‘ve managed to get one of the base modules to compile, but even in that small module I get 800 warnings of:
warning C4267: = conversion from size_t to int, possible loss of data
I understand why these appear, but what are my options for getting rid of them? Is there any systematic way that avoids touching every single instance?
One option is to disable the "loss of data" warning. To limit the effect of disabling the warning, MS Visual Studio has push
and pop
directives:
#pragma warning(push)
#pragma warning(disable: 4267)
// legacy code
#pragma warning(pop)
// normal code
These #pragma
directives are specific to Visual Studio; you might want to wrap them with #ifdef _MSC_VER
.
This is thought. I'm pretty sure +90% of those warning can be ignored. I had similar problem and lots of warning on something like this:
sumeType tab[10];
int items = std::size(tab);
// or
functionWhichExeptsInt(std::size(tab))
In above example since std::size
is a constexpr
compiler could just detect that size value is small enough to fit into int
so it should not report an warring but it does.
Problem is that there might be a cases where this warning can detect a real issue. So disabling this warning is not a good approach.
In my project we have decided to keep warring, but do not threat it as an error:
This is more like mental problem: "Can I ignore those +100 warning for now?". I also love code without warnings reported, but some times it is better to live with them.
IMO this is more safe approach.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With