將宏名稱作爲其他宏的參數來模擬高階函數是否「安全」?宏(高階)可以做多少?
I.e.我應該在哪裏尋找不要在自己的腳下拍自己?
下面是一些片段:
#define foreach_even(ii, instr) for(int ii = 0; ii < 100; ii += 2) { instr; }
#define foreach_odd(ii, instr) for(int ii = 1; ii < 100; ii += 2) { instr; }
#define sum(foreach_loop, accu) \
foreach_loop(ii, {accu += ii});
int acc = 0;
sum(foreach_even, acc);
sum(foreach_odd, acc);
什麼部分應用程序,我能做到嗎? :
#define foreach(ii, start, end, step, instr) \
for(int ii = start; ii < end; ii += step) { instr; }
#define foreach_even(ii, instr) foreach(ii, 0, 100, instr)
#define foreach_odd(ii, instr) foreach(ii, 1, 100, instr)
#define sum(foreach_loop, accu) \
foreach_loop(ii, {accu += ii});
int acc = 0;
sum(foreach_even, acc);
sum(foreach_odd, acc);
我可以在宏裏面定義一個宏嗎?
#define apply_first(new_macro, macro, arg) #define new_macro(x) macro(arg,x)