Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I partially disable C4244

In Visual C++ 2012 the code

double d = 0.5;
float f = d;
int i = f;

issues 2 warnings for me:

test.cpp(26): warning C4244: 'initializing' : conversion from 'double' to 'float', possible loss of data
test.cpp(27): warning C4244: 'initializing' : conversion from 'float' to 'int', possible loss of data

I want to suppress the first warning which I consider spam, but keep the second warning which I consider very helpful. Is it possible to suppress the one and keep the other? Do people generally just suppress them all? We had a bad bug where we mistakenly passed a double to a float. But we tons of our math code would trigger the double->float warnings.

like image 772
Philip Avatar asked Nov 09 '13 05:11

Philip


1 Answers

Don't suppress warnings that are designed to prevent potential bugs. Tell the compiler you know what you are doing instead by casting:

double d = 0.5;
float f = static_cast<float>(d);
int i = static_cast<int>(f);
like image 94
Casey Avatar answered Sep 20 '22 03:09

Casey