2

I saw this mechanism to simulate macro overloading recently here .

This is the code used for dispatching:

#define macro_dispatcher(func, ...) \
             macro_dispatcher_(func, VA_NUM_ARGS(__VA_ARGS__))
#define macro_dispatcher_(func, nargs) \
            macro_dispatcher__(func, nargs)
#define macro_dispatcher__(func, nargs) \
            func ## nargs

I don't understand how this works. Why does it need the third macro macro_dispatcher__ to concatenate the arguments? I have tried to eliminate the third macro and replace it with the second one, resulting this code:

 #include <stdio.h>
 #include "va_numargs.h"

 #define macro_dispatcher(func, ...) \
      macro_dispatcher_(func, __VA_NUM_ARGS__(__VA_ARGS__))

 #define macro_dispatcher_(func, nargs) \
     func ## nargs

 #define max(...) macro_dispatcher(max, __VA_ARGS__) \
      (__VA_ARGS__)                                    

 #define max1(a) a                                    
 #define max2(a, b) ((a) > (b) ? (a) : (b))

 int main()                                
 {                                         
     max(1);

     max(1, 2);

     return 0; 
 } 

va_numargs.h:

  #ifndef _VA_NARG_H
  #define _VA_NARG_H

  #define __VA_NUM_ARGS__(...) \
               PP_NARG_(__VA_ARGS__,PP_RSEQ_N())
  #define PP_NARG_(...) \
               PP_ARG_N(__VA_ARGS__)
  #define PP_ARG_N( \
                    _1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \
                   _11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
                   _21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
                   _31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
                   _41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
                   _51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
                   _61,_62,_63,N,...) N
  #define PP_RSEQ_N() \
               63,62,61,60,                   \
           59,58,57,56,55,54,53,52,51,50, \
           49,48,47,46,45,44,43,42,41,40, \
           39,38,37,36,35,34,33,32,31,30, \
           29,28,27,26,25,24,23,22,21,20, \
           19,18,17,16,15,14,13,12,11,10, \
           9,8,7,6,5,4,3,2,1,0

  #endif 

Which evaluates to this:

int main()
{
     max__VA_NUM_ARGS__(1) (1);

     max__VA_NUM_ARGS__(1, 2) (1, 2);

     return 0;
}

What is happening here? Why isn't __VA_NUM_ARGS__(__VA_ARGS__) replaced with the acutal number of arguments?

a3f
  • 8,517
  • 1
  • 41
  • 46
cristid9
  • 1,070
  • 1
  • 17
  • 37

1 Answers1

2

The extra step is needed because the token concatenation operator (##) suppresses macro expansion of its operands. Here's a simple example that demonstrates the problem:

#define macro macro_expansion
#define concat(x, y) x ## y

concat(macro, macro)

You might expect the above to produce macro_expansionmacro_expansion, but what you get instead is macromacro. While expanding the right-hand side of concat(), the preprocessor notices that x and y (which are set to macro here) are used as operands to ##, and so does not expand them further.

To work around this, we can add another step:

#define macro macro_expansion
#define concat(x, y) concat_(x, y)
#define concat_(x, y) x ## y

concat(macro, macro)

Now x and y are no longer operands of '##' in the right-hand side of concat(), and are therefore expanded. This means that we get concat_(macro_expansion, macro_expansion), which in turn expands to macro_expansionmacro_expansion.

The stringification operator (#) also suppresses macro expansion by the way.

Here's the relevant part of the C11 spec. (section 6.10.3.1):

A parameter in the replacement list, unless preceded by a # or ## preprocessing token or followed by a ## preprocessing token (see below), is replaced by the corresponding argument after all macros contained therein have been expanded.

Ulfalizer
  • 4,664
  • 1
  • 21
  • 30