0

I came across this code snippet. What's the purpose of #define and #undef a macro in an array initializer ?

#define MY_ARR MACRO(30)\ 
               MACRO(40) 
int array[] = { 
#define MACRO(a) a, 
                10, 
                20, 
             MY_ARR 
#undef MACRO 
};
ERROR
  • 9
  • 3
  • 3
    Where did you find such stuff? Without more context it doesn't make much sense. – Gerhardh Jun 30 '20 at 08:19
  • 3
    Are you sure there was no use of `MACRO` at all in the code you found? – Gerhardh Jun 30 '20 at 08:20
  • Guys, even i felt it's senseless before posting. But, the developer who wrote this code might have something in his mind. So, thought of checking with our community, if they can sense something. Thanks for your time guys. – ERROR Jun 30 '20 at 09:02
  • 1
    @ERROR It's not finished yet! Provide some more information. If possible, cite the source explicitly, so we can take a look at it. – RobertS supports Monica Cellio Jun 30 '20 at 09:11
  • @ERROR And is the shown snippet exactly equivalent to the source? – RobertS supports Monica Cellio Jun 30 '20 at 09:14
  • Often these kind of mysteries can be solved by looking at version control history. Perhaps something was removed at some point, and remaining code was left unused. – user694733 Jun 30 '20 at 09:56
  • Guys, pls accept my apologies. Here is the equivalent source code. #define MY_ARR MACRO(30)\ MACRO(40) int array[] = { #define MACRO(a) a, 10, 20, MY_ARR #undef MACRO }; – ERROR Jul 27 '20 at 06:50
  • @ERROR those are called X-macros, they're useful **only** if `MY_ARR` is used in **many** places... Also, if the `10, 20,` are on *separate lines* as in your example, it'd be better to just `#define MACRO(a)` just before `MY_ARR` is used. – Antti Haapala Jul 27 '20 at 07:38

3 Answers3

0

MACRO will only be known between its #define and its #undef

As MACRO is never used within its defined scope, it does nothing, so the result is

int array[]= {10, 20,};
Paul Ogilvie
  • 24,146
  • 4
  • 18
  • 39
0

There is no reason, the code is senseless. Since the macro is defined, immediately undefined and never called, this expands to:

int array[]= { 10, 20, };
Lundin
  • 155,020
  • 33
  • 213
  • 341
0

This function-like macro definition has no use because it is indeed not used anywhere in the array initializer until its undefinition at the end of the initializer list.

For example Clang complains this too:

"warning: macro is not used [-Wunused-macros]"

As result after the preprocessing, the code is equivalent to:

int array[] = { 10, 20, };

I guess the intention was to define a first element and initialize it with the value of a by the use of conditional compilation directives, so that array is composed of 3 elements instead of 2, with the first element containing the value of a.

This would be more appropriate for this case:

MACRO is defined before its use/check in the array initializer list. If it was defined, a is inserted into the initializer. Since MACRO has no further use, we can #undef it after the initialization.

#define MACRO 
#define a 5

int array[] = { 

    #ifdef MACRO 
        a,
    #endif
    #undef MACRO
        10,        
        20,        
};

Note: a could also be a const qualified integer object than a preprocessor macro: const int a = 5;.