Variadic Macros

  • Follow


Trying to find information on variadic macros.

Can you get specific parameters?

#define Test(...) printf("%s", ##__VAR_ARGS__)

I can't do this to get the first parameter

#define Test(...) printf("%s", var_arg(__VAR_ARGS__, char*))

or

#define Test(...) printf("%s", var_arg(*__VAR_ARGS__, char*))

or

#define Test(...) printf("%s", var_arg(*##__VAR_ARGS__, char*))




      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply michael782 (11) 3/6/2004 12:08:41 AM

Michael D. Borghardt wrote:
 > Trying to find information on variadic macros.

Variadic macros are not currently part of C++, so you should probably ask in a C
newsgroup.

 > Can you get specific parameters?

Yes, however sometimes a little preprocessor metaprogramming is required to
extract them.  Here's an example:

#define CAT(a, ...) PRIMITIVE_CAT(a, __VA_ARGS__)
#define PRIMITIVE_CAT(a, ...) a ## __VA_ARGS__

#define EXPAND(...) __VA_ARGS__

#define VARIADIC_ELEM(n, ...) \
     EXPAND( \
         PRIMITIVE_CAT(VARIADIC_ELEM_, n) \
         VARIADIC_ELEM_I(__VA_ARGS__,,,,,,,,,,) \
     ) \
     /**/
#define VARIADIC_ELEM_I(a, b, c, d, e, f, g, h, i, j, ...) \
     (a, b, c, d, e, f, g, h, i, j) \
     /**/
#define VARIADIC_ELEM_0(a, b, c, d, e, f, g, h, i, j) a
#define VARIADIC_ELEM_1(a, b, c, d, e, f, g, h, i, j)
#define VARIADIC_ELEM_1(a, b, c, d, e, f, g, h, i, j)
#define VARIADIC_ELEM_1(a, b, c, d, e, f, g, h, i, j)
#define VARIADIC_ELEM_1(a, b, c, d, e, f, g, h, i, j)
#define VARIADIC_ELEM_1(a, b, c, d, e, f, g, h, i, j)
#define VARIADIC_ELEM_1(a, b, c, d, e, f, g, h, i, j)
#define VARIADIC_ELEM_1(a, b, c, d, e, f, g, h, i, j)
#define VARIADIC_ELEM_1(a, b, c, d, e, f, g, h, i, j)



      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Paul 3/6/2004 9:59:56 AM


[Sorry to all for the double post; the other post was sent a bit early.]

{When this happens please send a message to the moderators, preferably 
with the sequence number you get when a post is acknowledged. I think 
that we moderators all read such mail before we start moderating posts. 
Posts often go the different moderators, and even when they go to the 
same one, the earlier post is likely to have been accepted unless warned 
in advance. -mod}

Michael D. Borghardt wrote:
 > Trying to find information on variadic macros.

Variadic macros are not currently part of C++, so you should probably ask in a C
newsgroup.

 > Can you get specific parameters?

Yes, however sometimes a little preprocessor metaprogramming is required to
extract them.  Here's an example:

#define CAT(a, ...) PRIMITIVE_CAT(a, __VA_ARGS__)
#define PRIMITIVE_CAT(a, ...) a ## __VA_ARGS__

#define EXPAND(...) __VA_ARGS__

#define VARIADIC_ELEM(n, ...) \
     EXPAND( \
         PRIMITIVE_CAT(VARIADIC_ELEM_, n) \
         VARIADIC_ELEM_I(__VA_ARGS__,,,,,,,,,,) \
     ) \
     /**/
#define VARIADIC_ELEM_I(a, b, c, d, e, f, g, h, i, j, ...) \
     (a, b, c, d, e, f, g, h, i, j) \
     /**/
#define VARIADIC_ELEM_0(a, b, c, d, e, f, g, h, i, j) a
#define VARIADIC_ELEM_1(a, b, c, d, e, f, g, h, i, j) b
#define VARIADIC_ELEM_2(a, b, c, d, e, f, g, h, i, j) c
#define VARIADIC_ELEM_3(a, b, c, d, e, f, g, h, i, j) d
#define VARIADIC_ELEM_4(a, b, c, d, e, f, g, h, i, j) e
#define VARIADIC_ELEM_5(a, b, c, d, e, f, g, h, i, j) f
#define VARIADIC_ELEM_6(a, b, c, d, e, f, g, h, i, j) g
#define VARIADIC_ELEM_7(a, b, c, d, e, f, g, h, i, j) h
#define VARIADIC_ELEM_8(a, b, c, d, e, f, g, h, i, j) i
#define VARIADIC_ELEM_9(a, b, c, d, e, f, g, h, i, j) j

VARIADIC_ELEM(0, +, -, *, /, %) // +
VARIADIC_ELEM(1, +, -, *, /, %) // -
VARIADIC_ELEM(2, +, -, *, /, %) // *
VARIADIC_ELEM(3, +, -, *, /, %) // /
VARIADIC_ELEM(4, +, -, *, /, %) // %

Regards,
Paul Mensonides



      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Paul 3/6/2004 10:04:34 AM

2 Replies
299 Views

(page loaded in 0.084 seconds)

Similiar Articles:











7/23/2012 10:45:28 AM


Reply: