Search code examples
macrosm4

M4 pushdef inside another macros context


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))

I expect the script will produce output like:

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

But instead of virtual int ma() = 0; virtual void mb() = 0; it returns line space-filled line. How should I define macros method at __expose_method evaluation time to get desired output?


Solution

  • The only way I found to implement this behavior is to use patsubst function in next manner:

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

    It seems to be duct tape.