问题
One header socket.h
on my Linux system looks like the following.
/* Bits in the FLAGS argument to `send', `recv', et al. */
enum
{
MSG_OOB = 0x01, /* Process out-of-band data. */
#define MSG_OOB MSG_OOB
MSG_PEEK = 0x02, /* Peek at incoming messages. */
#define MSG_PEEK MSG_PEEK
MSG_DONTROUTE = 0x04, /* Don't use local routing. */
#define MSG_DONTROUTE MSG_DONTROUTE
...
Defining an enum
is sort of an idiom for creating type-safe-ish constants in C that the language actually treats as compile-time constants.
My question is : what purpose does the definition of macros MSG_OOB
, MSG_PEEK
, … that expand to themselves serve?
回答1:
The POSIX standard mandates that "symbolic constants" such as MSG_DONTROUTE
be "object-like macros", rather than enumerants. Defining them to themselves allows them to be used in the context of the enumeration, as well as working properly with, e.g., #ifdef
.
From the POSIX standard:
The header shall define the following symbolic constants .... MSG_DONTROUTE
And:
symbolic constant... refers to a C preprocessor symbol (also without arguments).
And finally, from an appendix:
Where a constant is required to be a macro but is also allowed to be another type of constant such as an enumeration constant, on implementations which do define it as another type of constant the macro is typically defined as follows:
#define macro_name macro_name
This allows applications to use #ifdef, etc. to determine whether the macro is defined, but the macro is not usable in #if preprocessor directives because the preprocessor will treat the unexpanded word macro_name as having the value zero.
回答2:
The purpose of these defines is that the application can do something like
#ifdef MSG_OOB
some code here
#endif
expansion of macros is not recursive so the evaluation of macro MSG_OOB
results in the enum
constant MSG_OOB
of the same name.
To also have the constants declared as enum
helps for example when you are debugging.
来源:https://stackoverflow.com/questions/21855326/utility-of-macros-for-enum