There is justification for the warning, but this does not answer the whole picture. For example, the following code triggers a warning:
(int)round(M_PI);
but, on the other hand, the following code does not:
double d; (int)(d = round(M_PI));
this is also not:
(int)M_PI;
the rationale was that you should not convert to int by simple casting, but you should use round , floor or a similar function. However, using round will still raise a warning, but as you can see above, when pointing to a constant or assigned variable, no.
So, if itβs bad to do from double to int , then why the warning is not triggered when writing (int)d or (int)M_PI ? How should warnings be avoided if you want to convert the return value? Is there a warning that will handle these dangerous conversions in a more proper / reasonable way?
source share