实在是不知道怎么用一句标题描述这个问题。。
场景如下:
meta.h
#define META_FUNC_NAME(META_TYPE) func_## METY_TYPE
META_FUNC_NAME(SUPPLIED_META_TYPE)() {
do_something();
}
#undef SUPPLIED_META_TYPE
test.c:
#define SUPPLIED_META_TYPE float
#include "meta.h"
#define SUPPLIED_META_TYPE double
#include "meta.h"
我希望在 test.c 得到 func_double 和 func_float 两个函数。但是,上面的代码最终会展开出两个 func_METY_TYPE 函数:
In file included from test.c:5:
meta.h:1:35: error: redefinition of ‘func_METY_TYPE’
1 | #define META_FUNC_NAME(META_TYPE) func_## METY_TYPE
| ^~~~~
meta.h:2:6: note: in expansion of macro ‘META_FUNC_NAME’
2 | void META_FUNC_NAME(SUPPLIED_META_TYPE)() {
| ^~~~~~~~~~~~~~
In file included from test.c:2:
meta.h:1:35: note: previous definition of ‘func_METY_TYPE’ was here
1 | #define META_FUNC_NAME(META_TYPE) func_## METY_TYPE
| ^~~~~
meta.h:2:6: note: in expansion of macro ‘META_FUNC_NAME’
2 | void META_FUNC_NAME(SUPPLIED_META_TYPE)() {
| ^~~~~~~~~~~~~~
请问有什么解决方案吗?