宏函数可作为另一个宏函数的参数。见下面的例子代码。
//testmacro.cpp
#define DECLARE_ELEMENT(type, value, commit)\
type = value,
#define DECLARE_COMMON_ELEMENTS(F)\
F(TYINT32, 0, int)\
F(TYCHAR, 1, char)
enum BASE_TYPE
{
DECLARE_COMMON_ELEMENTS(DECLARE_ELEMENT)
};
int main()
{
BASE_TYPE t1 = TYINT32;
BASE_TYPE t2 = TYCHAR;
return 0;
}
预编译头文件
g++ -E testmacro.cpp -o pre.h
预编译结果
# 1 "testmacro.cpp"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
# 1 "testmacro.cpp"
# 9 "testmacro.cpp"
enum BASE_TYPE
{
TYINT32 = 0, TYCHAR = 1,
};
int main()
{
BASE_TYPE t1 = TYINT32;
BASE_TYPE t2 = TYCHAR;
return 0;
}