1

I am using the following sequence macro expansion. (I am not aware of any official term for it)

#define SEQ(F) F(1) F(2) F(3)

#define F1(I) HELLO_ ## I

SEQ(F1) // HELLO_1 HELLO_2 HELLO_3

It works as expected, but when I am trying to nest it into another macro expansion it doesn't work.

#define F2(I) SEQ(F1) WORLD_ ## I!!!

SEQ(F2) // SEQ(F1) WORLD_1!!! SEQ(F1) WORLD_2!!! SEQ(F1) WORLD_3!!!

Instead, I expect it to expand to

HELLO_1 HELLO_2 HELLO_3 WORLD_1!!! HELLO_1 HELLO_2 HELLO_3 WORLD_2!!! HELLO_1 HELLO_2 HELLO_3 WORLD_3!!!

Why it doesn't work and how to fix it?


Update:

It seems the same mechanism that forbids the use of recursive macro breaks things for me: the preprocessor thinks that SEQ is already expanded in SEQ(F2) so it doesn't attempt to expand SEQ(F1).

My workaround is to copy-paste SEQ to SEQ1

#define SEQ(F) F(1) F(2) F(3)
#define SEQ1(F) F(1) F(2) F(3)

and then use

#define F2(I) SEQ1(F1) WORLD_ ## I!!!
SEQ(F2)
Curious
  • 507
  • 3
  • 16
  • It doesn't work because macro expansion is not recursive. You need to devise a solution that does not depend on recursing (of the `SEQ` macro in this case). – John Bollinger Jun 06 '21 at 13:52

0 Answers0