这个宏调度器是如何工作的?
How does this macro dispatcher work?
最近看到这个模拟宏重载的机制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__)
没有替换为实际的参数个数?
需要额外的步骤,因为标记连接运算符 (##
) 禁止对其操作数进行宏扩展。这是一个演示问题的简单示例:
#define macro macro_expansion
#define concat(x, y) x ## y
concat(macro, macro)
您可能希望上面的结果产生 macro_expansionmacro_expansion
,但您得到的却是 macromacro
。在扩展 concat()
的右侧时,预处理器注意到 x
和 y
(此处设置为 macro
)用作 [=12= 的操作数],因此不会进一步扩展它们。
要解决这个问题,我们可以添加另一个步骤:
#define macro macro_expansion
#define concat(x, y) concat_(x, y)
#define concat_(x, y) x ## y
concat(macro, macro)
现在 x
和 y
不再是 concat()
右侧 '##' 的操作数,因此被展开。这意味着我们得到 concat_(macro_expansion, macro_expansion)
,它又扩展为 macro_expansionmacro_expansion
.
字符串化运算符(#
)也顺便抑制了宏展开。
这是 C11 规范的相关部分。 (第 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.
最近看到这个模拟宏重载的机制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__)
没有替换为实际的参数个数?
需要额外的步骤,因为标记连接运算符 (##
) 禁止对其操作数进行宏扩展。这是一个演示问题的简单示例:
#define macro macro_expansion
#define concat(x, y) x ## y
concat(macro, macro)
您可能希望上面的结果产生 macro_expansionmacro_expansion
,但您得到的却是 macromacro
。在扩展 concat()
的右侧时,预处理器注意到 x
和 y
(此处设置为 macro
)用作 [=12= 的操作数],因此不会进一步扩展它们。
要解决这个问题,我们可以添加另一个步骤:
#define macro macro_expansion
#define concat(x, y) concat_(x, y)
#define concat_(x, y) x ## y
concat(macro, macro)
现在 x
和 y
不再是 concat()
右侧 '##' 的操作数,因此被展开。这意味着我们得到 concat_(macro_expansion, macro_expansion)
,它又扩展为 macro_expansionmacro_expansion
.
字符串化运算符(#
)也顺便抑制了宏展开。
这是 C11 规范的相关部分。 (第 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.