使用另一个宏的宏的奇怪行为

Strange behavior of a macro that uses another macro

我对 C++ 中的宏有疑问;

我的代码:

#define a [i]
#define b(i) t a

int main(){
 int t[10];
 int i=0;
 b(i+1)=1;
}

预处理器完成工作后的预期结果:

int main(){
 int t[10];
 int i=0;
 t[i+1]=1;
}

实际结果:

int main(){
 int t[10];
 int i=0;
 t[i]=1;
}

我明白发生了什么,但是有什么方法可以强制预处理器做我想做的事吗? (所以首先替换宏 b 中的代码,而不是将此代码解释为宏的一部分?)

这组宏产生了预期的结果。

#define a(i) [i]
#define b(i) t a(i)

示例test.cpp

#define a(i) [i]
#define b(i) t a(i)

int main(){
 int t[10];
 int i=0;
 b(i+1)=1;
}

然后使用 g++ -E test.cpp 输出是:

# 1 "test.cpp"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
# 1 "test.cpp"



int main(){
 int t[10];
 int i=0;
 t [i+1]=1;
}