Macros on C++ style

Is it ok to define these in this way? Are these following C++ convention?
If not please direct me how

#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)
UBYTE and ULONG would be better as typedefs:
1
2
typedef unsigned char UBYTE;
typedef unsigned long ULONG;

You probably don't need FALSE since type bool is part of C++ and includes the constants true and false.

COMPRESS_MAX_COM would be better as a const int:
const int COMPRESS_MAX_COM = 0x70000000;

COMPRESS_MAX_ORG and MAX_RAW_GROUP should probably be const int also, assuming that COMPRESS_OVERRUN and MAX_RAW_ITEM are both consts.

U(X) could be done as a templated function. This would remove the nasty bug of something like U(18+2.2) which expands to ((ULONG)18+2.2) which is a double with value 20.2. If you do this as a macro, it should be
#define U(X) ((ULONG)(X))
typedef unsigned char UBYTE
typedef unsigned long ULONG;
const int COMPRESS_MAX_COM = 0x70000000;
const COMPRESS_MAX_ORG (const 1024-COMPRESS_OVERRUN)
const MAX_RAW_GROUP (const 16*MAX_RAW_ITEM)

is it ok now?

And templates function? you mean?
Try this:
1
2
3
4
5
6
7
typedef unsigned char UBYTE
typedef unsigned long ULONG;
const int COMPRESS_MAX_COM = 0x70000000;
const int COMPRESS_MAX_ORG = (1024-COMPRESS_OVERRUN);
const int MAX_RAW_GROUP = (16*MAX_RAW_ITEM);

#define U(X) ((ULONG)(X)) 
thanks
May I ask why you are doing this?

This kind of stuff belongs to legacy code and, particularly in the case of UBYTE and ULONG, to common extant libraries.

The U() macro is ever appropriate outside of deliberate obfuscation.
It is just to learn c++ style macro,
As already demonstrated, there are better alternatives. Macros have their place, but it is a limited one in C++.

When defining stuff that is common, like FALSE (you may get it in stdio.h!), make sure to guard it properly:

1
2
3
#ifndef FALSE
#define FALSE (0)
#endif 

I think a good general rule is: macros should be used to select code, not to generate it.

To generate code use templates.

By selecting code I mean of course using conditionals and symbols that only act as flags to the preprocessor and have no real inpact in your code. This keeps things uniform (or at least as uniform as they can be in C++).
Topic archived. No new replies allowed.