Question

How can I rewrite the following macro so that they actually followed c++ convention? (that in C++, we prefer to use typedef, const and inline functions).
Here is the macro.

#define UBYTE unsigned char
#define ULONG unsigned long
#define FALSE 0
#define COMPRESS_MAX_COM 0x70000000
#define COMPRESS_MAX_ORG (1024-COMPRESS_OVERRUN)
#define MAX_RAW_GROUP (16*MAX_RAW_ITEM)
#define U(X) ((ULONG) X)

How can I change the above macro into the c++ convention?

Was it helpful?

Solution

Use typedef or using (C++11) instead of #define for the types, then use const for constants. The functions can be defined as constexpr in C++11.

typedef unsigned char UBYTE;  // using UBYTE = unsigned char; // C++11
typedef unsigned long ULONG;
const unsigned int FALSE=0; // constexpr usigned int FALSE = 0; // C++11
const ULONG COMPRESS_MAX_COM=0x70000000;
const ULONG COMPRESS_MAX_ORG=(1024-COMPRESS_OVERRUN); // if COMPRESS_OVERRUN is const
const ULONG MAX_RAW_GROUP=(16*MAX_RAW_ITEM); // again if MAX_RAW_ITEM is a previously defined const
#define U(X) ((ULONG) X) // this can stay as it is

or

template <typename T>
ULONG U(const T& x)
{
    return static_cast<ULONG>(x);
}

so U(some type T) will return a cast-to-ULONG of its argument of type T.

OTHER TIPS

You should consider integer size, especially if you are using these definitions in an interface.

For example:

#include <stdint.h>
typedef uint8_t UBYTE;  // using UBYTE = unsigned char; // C++11
typedef uint32_t ULONG;
const uint32_t FALSE=0; // constexpr unsigned int FALSE = 0; // C++11

The other definitions @vsoftco gave above are great:

const ULONG COMPRESS_MAX_COM=0x70000000;
const ULONG COMPRESS_MAX_ORG=(1024-COMPRESS_OVERRUN); // if COMPRESS_OVERRUN is const
const ULONG MAX_RAW_GROUP=(16*MAX_RAW_ITEM); // again if MAX_RAW_ITEM is a previously defined const
#define U(X) ((ULONG) X) // this can stay as it is
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top