首页 > 解决方案 > 另一个宏上下文中的 M4 pushdef

问题描述

define(`__for_each', 
    `ifelse(`$#', `1', `', `$#', `2', `$1(`$2')', 
`$1(`$2')__for_each(`$1', shift(shift($@)))')')dnl
define(`__method_decl', `virtual $2 $1() = 0;')
define(`__expose_method', `pushdef(`method', `__method_decl')$1 popdef(`method')')dnl
define(`interface', ``struct' $1 { 
__for_each(`__expose_method', shift($@))
};')dnl
interface(iface, 
    method(ma, int), 
    method(mb, void))

我希望脚本会产生如下输出:

struct iface { 
virtual int ma() = 0; virtual void mb() = 0; 
};

virtual int ma() = 0; virtual void mb() = 0;它不是返回行空间填充的行。method我应该如何在评估时定义宏__expose_method以获得所需的输出?

标签: macrosm4

解决方案


我发现实现此行为的唯一方法是以以下patsubst方式使用函数:

define(`__expose_method', `patsubst(`$1', `method', `__method_decl')')dnl

好像是胶带。


推荐阅读