Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Utility of macros for enum

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?

like image 640
Pascal Cuoq Avatar asked Feb 18 '14 13:02

Pascal Cuoq


People also ask

Is an enum a macro?

Differences between Enumeration and Macro are: Enumeration is a type of integer. Macros can be of any type. Macros can even be any code block containing statements, loops, function calls etc.

What is the use of enum in programming?

An enumeration, or Enum , is a symbolic name for a set of values. Enumerations are treated as data types, and you can use them to create sets of constants for use with variables and properties.

Are enumerators the same as macros?

Explanation: Enumerators are used in order to create our own types whereas macros are textual substitutions.

What is enum statement?

Defines a user-defined enumeration (also known as an enumerated type or enum). An enum is made up of a list of strongly typed, named constants called members. The value of a variable defined as an enum is restricted to the list of members defined for that enum.


2 Answers

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.

like image 67
Jens Gustedt Avatar answered Oct 10 '22 05:10

Jens Gustedt


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.

like image 25
Sneftel Avatar answered Oct 10 '22 04:10

Sneftel