【问题标题】:How does this macro dispatcher work?这个宏调度器是如何工作的?
【发布时间】:2015-03-08 12:32:59
【问题描述】:

我最近看到了这种模拟宏重载的机制here

这是用于调度的代码:

#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

我不明白这是如何工作的。为什么需要第三个宏macro_dispatcher__ 来连接参数?我试图消除第三个宏并用第二个宏替换它,结果代码如下:

 #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 

计算结果如下:

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

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

     return 0;
}

这里发生了什么?为什么__VA_NUM_ARGS__(__VA_ARGS__) 不替换为实际的参数数量?

【问题讨论】:

    标签: c c-preprocessor


    【解决方案1】:

    需要额外的步骤,因为标记连接运算符 (##) 抑制了其操作数的宏扩展。这是一个演示问题的简单示例:

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

    您可能希望上面生成macro_expansionmacro_expansion,但您得到的是macromacro。在扩展concat() 的右侧时,预处理器注意到xy(此处设置为macro)用作## 的操作数,因此不会进一步扩展它们。

    要解决这个问题,我们可以添加另一个步骤:

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

    现在xy 不再是concat() 右侧“##”的操作数,因此被扩展。这意味着我们得到concat_(macro_expansion, macro_expansion),它又扩展为macro_expansionmacro_expansion

    字符串化操作符 (#) 顺便也抑制了宏扩展。

    这是 C11 规范的相关部分。 (第 6.10.3.1 节):

    替换列表中的参数,除非前面有 ### 预处理令牌或后跟 ## 预处理令牌(见下文),否则将被相应的参数替换在其中包含的所有宏都已展开之后。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多