LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   #defines inside an enum (https://www.linuxquestions.org/questions/programming-9/defines-inside-an-enum-4175430037/)

wcrandthor 10-02-2012 07:19 AM

#defines inside an enum
 
I was looking at linux/include/linux/rtnetlink.h

Code:

enum {
        RTM_BASE        = 16,
#define RTM_BASE        RTM_BASE

        RTM_NEWLINK        = 16,
#define RTM_NEWLINK        RTM_NEWLINK

Why are these #defines embedded within the enum here? What's the point of this type of code?
Note I'm not talking about this rtnetlink.h functionality specifically... just in general, the only place I've seen #defines embedded in enum's is in linux code.

johnsfine 10-03-2012 03:54 PM

Those #defines seem to be intended to sort of undefine those symbols as preprocessor symbols right after potentially using the preprocessor symbols to define names in the enum.

Out of context, I'm failing to guess the intent.

The preprocessor acts as if it were executed before the rest of compilation, so the preprocessor does not care that the #define happens to land inside the body of the enum definition.

Edit: After using google, I have a better guess (mainly expanding on the answer that you seem to have gotten when asking the same question in a different forum):

Assuming there are many variants of rtnetlink.h floating around (for different OS's etc.) and some of those defined RTM_BASE by #define and some didn't define it at all, then user code depending on rtnetlink.h but wanting to be portable would include code such as
Code:

#ifdef RTM_BASE
to see if RTM_BASE is defined. But then a version, such as you quoted, that defines it by enum would break such user code, so redundantly define it by #define (as itself) to allow that test.

NevemTeve 10-03-2012 04:34 PM

Preprocesssor does run in a separated process before the compilation, and yes, these defines are meant for #ifdef's.


All times are GMT -5. The time now is 07:35 PM.