There's a rationale for the warning here, but that fails to answer the whole picture. For example the following code triggers the warning:
(int)round(M_PI);
but on the other hand the following code doesn't:
double d;
(int)(d = round(M_PI));
this doesn't either:
(int)M_PI;
the rationale was that you shouldn't convert to int by simply casting, but you should use round
, floor
or similar function. However using round
will still trigger the warning, but as seen above typecasting a constant or assigned variable doesn't.
So if it's that bad to cast from double
to int
, then why doesn't the warning trigger when you write (int)d
or (int)M_PI
? In what way is one supposed to circumvent the warning in case you want to convert the return value? Is there a warning that would handle these perilous conversions in a more correct/reasonable way?