简体   繁体   English

(高阶)宏可以处理多少?

[英]How much one can do with (higher order) macros?

Is it "safe" to give macros names as arguments to other macros to simulate higher order functions? 将宏名称作为其他宏的参数来模拟高阶函数是否“安全”?

Ie where should I look to not shoot myself in the foot? 即我应该在哪里看不到自己的脚?

Here are some snippets: 以下是一些片段:

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

What about partial application, can I do that? 部分应用怎么样,我可以这样做吗? :

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

And can I define a macro inside a macro? 我可以在宏中定义一个宏吗?

#define apply_first(new_macro, macro, arg) #define new_macro(x) macro(arg,x)

If you're into using preprocessor as much as possible, you may want to try boost.preprocessor . 如果您正在尽可能多地使用预处理器,则可能需要尝试使用boost.preprocessor

But be aware that it is not safe to do so. 但请注意,这样做并不安全 Commas, for instance, cause a great number of problems when using preprocessors. 例如,逗号在使用预处理器时会导致很多问题。 Don't forget that preprocessors do not understand (or even try to understand) any of the code they are generating. 不要忘记预处理器不理解(甚至试图理解)它们生成的任何代码。

My basic advice is "don't do it", or "do it as cautiously as possible". 我的基本建议是“不要这样做”,或“尽可能谨慎地做”。

I've implemented a rotten little unit testing framework entirely in c-preprocessor. 我完全在c-preprocessor中实现了一个烂小的单元测试框架。 Several dozen macro, lots of macro is an argument to another macro type stuff. 几十个宏,很多宏是另一个宏类型的东西的参数。

This kind of thing is not "safe" in a best-practices sense of the word. 这种事情在最佳实践意义上并非 “安全”。 There are subtle and very powerful ways to shoot yourself in the foot. 有一些微妙而且非常强大的方法来射击自己的脚。 The unit testing project is a toy that got out of hand. 单元测试项目是一个失控的玩具。

Don't know if you can nest macro definitions. 不知道你是否可以嵌套宏定义。 I doubt it, but I'll go try...gcc doesn't like it, and responds with 我对此表示怀疑,但我会去尝试... gcc不喜欢它,并回应

nested_macro.cc:8: error: stray '#' in program nested_macro.cc:8:错误:在程序中迷路'#'
nested_macro.cc:3: error: expected constructor, destructor, or type conversion before '(' token nested_macro.cc:3:错误:在'('标记之前)的预期构造函数,析构函数或类型转换
nested_macro.cc:3: error: expected declaration before '}' token nested_macro.cc:3:错误:在'}'标记之前的预期声明

Self plug : If you're interested the unit testing framework can be found at https://sourceforge.net/projects/dut/ 自插 :如果您对单元测试框架感兴趣, 访问https://sourceforge.net/projects/dut/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何比较高阶订单类型? - How do I compare higher order types? 如何检查单个#ifdef中是否定义了多个宏之一? - How do I check if one of multiple macros is defined in a single #ifdef? 如何比较一个字符的字母顺序是高还是低? - How can I compare if a char is higher or lower in alphabetical order than another? 给定一组字符,如何仅通过重新排列字符来显示具有更高字典顺序的字符串? - Given a set of characters, how can you display the string with a higher lexicographical order, only by rearranging the characters? 窗口是否会收到三次点击事件? 如何处理高阶点击? - Does a window receive triple click events? How can I handle higher-order clicks? 如何使用宏或元编程在 C++ 中创建“passthru”函数? - How can one make a 'passthru' function in C++ using macros or metaprogramming? 如何使用宏进行static_assert? - How to do static_assert with macros? 当`healt`不能高于`maxHealth`时,如何在一个对象中定义`health`和`maxHealth`? - How to define `health` and `maxHealth` in one object, when `healt` can't be higher than `maxHealth`? 宏参数顺序很重要吗? - Macros argument order matters? Doxygen:我如何记录非#defined的宏? - Doxygen: How can I document macros that are not #defined?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM