c-preprocessorvariadic-macros

Using __VA_ARGS__ in a nested macro, but the args are truncated


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.


Solution

  • 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.