mardi 29 janvier 2019

How to correct this code to avoid old-style-cast warnings with C++

I'm new to c++ and I'm trying to avoid old-style-cast warning in a piece of code during compilation. The code is a part of an old opensource code. Here is the code itself:

#define ROUND_TO_BYTE(X)      ((BYTE)((X)+0.499))
#define ROUNDS(X)             ((short)((X)+0.499))
#define TRUNC(X)              ((short) (X))

#define CONVERT_DOUBLE_TO_FLOAT(val) \
  ( (val) >= SMALLFLOAT \
    ? ( (val) < LARGEFLOAT \
        ? (float)(val) \
        : (float)LARGEFLOAT \
      ) \
    : ( (val) <= -SMALLFLOAT  \
        ? ( (val) > -LARGEFLOAT \
            ? (float)(val) \
            : (float)-LARGEFLOAT \
          ) \
        : (float)0.0 \
      ) \
  )

I have tried as follow and the code does compile without any warning:

#define ROUND_TO_BYTE(X)      (static_cast<BYTE>((X)+0.499))
#define ROUNDS(X)             (static_cast<short>((X)+0.499))
#define TRUNC(X)              (static_cast<short>(X))


#define CONVERT_DOUBLE_TO_FLOAT(val) \
  ( (val) >= SMALLFLOAT \
    ? ( (val) < LARGEFLOAT \
        ? static_cast<float>(val) \
        : static_cast<float>(LARGEFLOAT) \
      ) \
    : ( (val) <= -SMALLFLOAT  \
        ? ( (val) > -LARGEFLOAT \
            ? static_cast<float>(val) \
            : static_cast<float>(-LARGEFLOAT) \
          ) \
        : static_cast<float>(0.0) \
      ) \
  )

However I'm not sure if I get the following lines right:

#define TRUNC(X)              ((short) (X))

to

#define TRUNC(X)              (static_cast<short>(X))

and

        ? (float)(val) \

to

        ? static_cast<float>(val) \

I don't understand why using (float)(val) instead of (float)val and whether I need to change the code to:

#define TRUNC(X)              (static_cast<short>((X)))
? static_cast<float>((val)) \

Aucun commentaire:

Enregistrer un commentaire