Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Macro count params

In order to make compiler happy I have to count params passed to A(), otherwise gcc raises "warning: ISO C99 requires rest arguments to be used" when pedantic flag is on and only one param is passed

#include <stdio.h>

/* Count params */
#define N_ARGS(...) N_ARGS_IMPL(__VA_ARGS__,n,n,n,n,n,n,n,n,n,1,1)
#define N_ARGS_IMPL(_0,_1,_2,_3,_4,_5,_6,_7,_8,_9,N,...) N

#define A_fmt_void
#define A_arg_void

/* link */
#define A_fmt_link_1(fmt) " href=\""fmt"\""
#define A_fmt_link_n(fmt, ...) " href=\""fmt"\""
#define A_fmt_link_N(n, ...) A_fmt_link_##n(__VA_ARGS__)
#define A_fmt_link_X(n, ...) A_fmt_link_N(n,__VA_ARGS__)
#define A_fmt_link(...) A_fmt_link_X(N_ARGS(__VA_ARGS__), __VA_ARGS__)

#define A_arg_link_1(fmt)
#define A_arg_link_n(fmt, ...) , __VA_ARGS__
#define A_arg_link_N(n, ...) A_arg_link_##n(__VA_ARGS__)
#define A_arg_link_X(n, ...) A_arg_link_N(n,__VA_ARGS__)
#define A_arg_link(...) A_arg_link_X(N_ARGS(__VA_ARGS__), __VA_ARGS__)

/* text */
#define A_fmt_text_1(fmt) fmt
#define A_fmt_text_n(fmt, ...) fmt
#define A_fmt_text_N(n, ...) A_fmt_text_##n(__VA_ARGS__)
#define A_fmt_text_X(n, ...) A_fmt_text_N(n,__VA_ARGS__)
#define A_fmt_text(...) A_fmt_text_X(N_ARGS(__VA_ARGS__), __VA_ARGS__)

#define A_arg_text_1(fmt)
#define A_arg_text_n(fmt, ...) , __VA_ARGS__
#define A_arg_text_N(n, ...) A_arg_text_##n(__VA_ARGS__)
#define A_arg_text_X(n, ...) A_arg_text_N(n,__VA_ARGS__)
#define A_arg_text(...) A_arg_text_X(N_ARGS(__VA_ARGS__), __VA_ARGS__)

/* macro */
#define A(link, text) \
    printf("<a"A_fmt_##link">"A_fmt_##text"</a>\n" A_arg_##link A_arg_##text)

int main(void)
{
    A(link(), void);
    A(void, text());
    A(link("http://www.google.es"), void);
    A(link("%s/%s", "http://www.google.es", "home"), text("Visit google"));
    A(void, text("%s today", "Visit google"));
    A(link("http://%s/%s", "www.google.es", "home"), text("%s today", "Visit google"));
    A(void,void);
    return 0;
}

With this implementation of N_ARGS I can use only 10 params, is there another way to check if there is more than one param without limit in macro?

I know , ## __VA_ARGS__ gcc extension but I want to avoid warnings

like image 916
David Ranieri Avatar asked Jan 04 '13 10:01

David Ranieri


People also ask

What is __ Va_args __ in C++?

To use variadic macros, the ellipsis may be specified as the final formal argument in a macro definition, and the replacement identifier __VA_ARGS__ may be used in the definition to insert the extra arguments. __VA_ARGS__ is replaced by all of the arguments that match the ellipsis, including commas between them.

Can macros have parameters?

Function-like macros can take arguments, just like true functions. To define a macro that uses arguments, you insert parameters between the pair of parentheses in the macro definition that make the macro function-like. The parameters must be valid C identifiers, separated by commas and optionally whitespace.

How many arguments can a macro have?

For portability, you should not have more than 31 parameters for a macro. The parameter list may end with an ellipsis (…).


1 Answers

Finally I've fixed using __ extension__

#include <stdio.h>

#define A_fmt_void
#define A_arg_void
#define A_fmt_link(fmt, ...) " href=\""fmt"\""
#define A_arg_link(fmt, ...) , ## __VA_ARGS__
#define A_fmt_text(fmt, ...) fmt
#define A_arg_text(fmt, ...) , ## __VA_ARGS__
#define A(link, text) \
    __extension__ printf("<a" A_fmt_##link ">" A_fmt_##text "</a>\n" A_arg_##link A_arg_##text)

int main(void)
{
    A(
        link("%s", "http://wwww.google.com"),
        text("%s", "Visit google")
    );
    A(
        link("http://wwww.google.com"),
        void
    );
    A(
        void,
        text("Visit google")
    );
    A(
        void,
        void
    );
    return 0;
}

This prevents warnings when pedantic flag is on :)

like image 197
David Ranieri Avatar answered Sep 30 '22 22:09

David Ranieri