#define PACKAGE Package123
#define TEST_SUITE_NAME_PACKAGE(Suite,Package) Suite##_##Package
#define TEST_SUITE_NAME(Suite) TEST_SUITE_NAME_PACKAGE(Suite,PACKAGE)
#define SUITE_NAME TEST_SUITE_NAME(UtilitiesTest)
我希望SUITE_NAME
评估为UtilitiesTest_Package123
,但经过多次变更后,我仍然会UtilitiesTest_PACKAGE
。
注意:SUITE_NAME
将用作标识符,而不是字符串。我和#
和##
运算符混在一起,但似乎没什么用。
答案 0 :(得分:1)
预处理器有时需要数量惊人的间接:
#define PACKAGE Package123
#define PASTE(x,y) x##_##y
#define TEST_SUITE_NAME_PACKAGE(Suite,Package) PASTE(Suite,Package)
#define TEST_SUITE_NAME(Suite) TEST_SUITE_NAME_PACKAGE(Suite,PACKAGE)
#define SUITE_NAME TEST_SUITE_NAME(UtilitiesTest)
对于这种情况,这种PASTE
方法是常见的预处理技巧。
答案 1 :(得分:0)
只需添加另一个间接级别
#define PACKAGE Package123
#define TEST_SUITE_NAME_PACKAGE(Suite,Package) Suite##_##Package
#define _TEST_SUITE_NAME_PACKAGE(Suite, PACKAGE) TEST_SUITE_NAME_PACKAGE(Suite,PACKAGE)
#define TEST_SUITE_NAME(Suite) _TEST_SUITE_NAME_PACKAGE(Suite,PACKAGE)
#define SUITE_NAME TEST_SUITE_NAME(UtilitiesTest)