0

I'm doing something like below:

#define AA(mac, a, ...) mac(a, __VA_ARGS__)
#define MAC1(a, b, c) a##b##c

AA(MAC1, 0, 1, 2)

what I really want is to translate "AA(MAC1, 0, 1, 2)" to "012", but I get "01,2", which is reasonalbe though, but not I want.

Edit: A work around is to remove the VA_ARGS, and define a AA like

#define AA(mac,a,b,c,d,e,f,g,h,...) mac(a,b,c,d,e,f,g,h)

#define MAC1(a, b, c) a##b##c

AA(MAC1, 0, 1, 2)

gives what I what, "012", I don't know why though.

Alexis
  • 145
  • 8
  • This is probably an issue with the evaluation order. It seems like MAC1 is evaluated on "1,2" as a single argument rather than as two arguments. Maybe try playing with something like an `#define EVALUATE(...) __VA_ARGS__` macro and adding it into the mix. – einpoklum Apr 16 '22 at 09:14
  • @einpoklum, hi, I'm not sure I'm getting your idea correct, but AA(MAC1, 0, EVALUATE(1, 2)) also gives 01,2 – Alexis Apr 16 '22 at 09:33

1 Answers1

0

Your initial code:

#define AA(mac, a, ...) mac(a, __VA_ARGS__)
#define MAC1(a, b, c) a##b##c

AA(MAC1, 0, 1, 2)

seems to work fine:

$ gcc -E a.c > a.pp ; cat a.pp
# 1 "a.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 31 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 32 "<command-line>" 2
# 1 "a.c"



012

So, maybe there's an issue with your compiler or IDE.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • Thank you very much, I'm using vs2022, I think I should use less compiler-specific features. – Alexis Apr 17 '22 at 19:01
  • @Alexis: You can try using the [Compiler Explorer website](http://godbolt.org/) to check how different compilers behave with your code. – einpoklum Apr 17 '22 at 19:32
  • thank you very much! Using AA(MAC1, i, n, t) a = 0; at godbolt.org with -E indeed shows that this works with gcc, clang but not msvc! That's very useful! – Alexis Apr 17 '22 at 19:44