有没有办法恢复 C 中的宏,以便您可以定义一个新的宏,在该宏的名称下可能已经定义了其他宏,并使用以前的值重新定义它?
这样当新定义的宏被删除并且最终重新定义的宏被重置为之前的状态时?
例子:
// a macro parameter used in a library
#define size 10
#include <library/use_size.h>
//here the command/pragma to save the definitions
#define size (100 / sizeof(size_t))
// some use of size ...
//here the command/pragma to reset the definitions
#include <library/allocator_with_size.h>
#undef size
// use size as a variable name
size_t size = 0;
//...
size += 123;
Run Code Online (Sandbox Code Playgroud)
编辑:我不想使用#undef,因为它不会恢复旧的宏。另外,如果你有很多宏,例如在 X 宏列表中使用它们(在常量数组和结构的长重复代码/声明中),如果有很多#undef指令,它看起来很丑陋。