preprocessor and parenthesis

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • effbiae

    #1

    preprocessor and parenthesis

    hi,

    this code:

    #define M(a,b) static int is[3]={a,b;
    M(1,(2,3)})

    produces this (using gcc -E):

    static int is[3]={1,(2,3)};

    which gcc does not complain about.

    my question(s):
    o is there a better way of passing a comma separated list as the 'b'
    parameter
    o is gcc being generous by
    a) preprocessing this
    b) compiling it
    o how can i write a macro like this:

    #define F(a,b) int a(obj*o b

    so that i can write function declarations in which all functions have a
    first param obj, but may optionally have more.

    thanks,


    jack
    ivorykite.com

  • Harald van Dijk

    #2
    Re: preprocessor and parenthesis

    effbiae wrote:[color=blue]
    > hi,[/color]

    Hi,
    [color=blue]
    > this code:
    >
    > #define M(a,b) static int is[3]={a,b;
    > M(1,(2,3)})[/color]

    How about moving the } to the macro definition?

    #define M(a,b) static int is[3]={a,b};
    M(1,(2,3))

    Isn't that more readable?
    [color=blue]
    > produces this (using gcc -E):
    >
    > static int is[3]={1,(2,3)};
    >
    > which gcc does not complain about.[/color]

    gcc 3.4 and 4 do complain about that no matter what, and older versions
    complain about it with -pedantic. Additionally, on those older
    versions, it probably doesn't do what you expect: it initialises is[0]
    with 1, and is[1] with (2,3), which evaluates to 3.
    [color=blue]
    > my question(s):
    > o is there a better way of passing a comma separated list as the 'b'
    > parameter[/color]

    Variadic macros.

    #define M(...) static int is[3]={__VA_ARGS__};
    M(1,2,3)

    Or, if you want to enforce a minimum of two arguments:

    #define M(a, ...) static int is[3]={a, __VA_ARGS__};
    M(1,2,3)
    [color=blue]
    > o is gcc being generous by
    > a) preprocessing this[/color]

    Nope.
    [color=blue]
    > b) compiling it[/color]

    Yep.
    [color=blue]
    > o how can i write a macro like this:
    >
    > #define F(a,b) int a(obj*o b
    >
    > so that i can write function declarations in which all functions have a
    > first param obj, but may optionally have more.[/color]

    Again, why not put the ) in the macro definition?

    #define F(a,b) int a(obj*o b)

    gcc has a non-standard extension that makes this easy -- refer to the
    documentation if you're interested -- but with standard C it, while
    possible, is needlessly complex since you don't know in advance whether
    you'll need a comma after o. The easiest route is to create two macros:

    #define F_0(a) int a(obj* o)
    #define F_1(a,...) int a(obj* o, __VA_ARGS__)

    and choose the appropriate one by hand each time.

    HTH

    Comment

    • effbiae

      #3
      Re: preprocessor and parenthesis

      Harald wrote:

      thanks for your help.
      [color=blue]
      > Variadic macros.[/color]

      of course...
      [color=blue]
      > you'll need a comma after o. The easiest route is to create two macros:
      >
      > #define F_0(a) int a(obj* o)
      > #define F_1(a,...) int a(obj* o, __VA_ARGS__)
      >
      > and choose the appropriate one by hand each time.[/color]

      i think i could do something like this:
      #define F(a) int a(obj*o,...)
      and leave the argument-getting up to the function

      do i pay an efficiency penalty for using va_list macros/functions?

      thanks,



      jack

      Comment

      • Harald van Dijk

        #4
        Re: preprocessor and parenthesis

        effbiae wrote:[color=blue]
        > i think i could do something like this:
        > #define F(a) int a(obj*o,...)
        > and leave the argument-getting up to the function[/color]

        Yeah, that should work. You may now accidentally call the functions
        with the wrong number of arguments, of course, so be careful about
        that.
        [color=blue]
        > do i pay an efficiency penalty for using va_list macros/functions?[/color]

        In theory, yes, but it's nothing likely to be noticeable. If it makes
        things easier, go for it.

        Comment

        • tmp123

          #5
          Re: preprocessor and parenthesis


          effbiae wrote:[color=blue]
          > hi,
          >
          > this code:
          >
          > #define M(a,b) static int is[3]={a,b;
          > M(1,(2,3)})
          >
          > produces this (using gcc -E):
          >
          > static int is[3]={1,(2,3)};
          >
          > which gcc does not complain about.
          >
          > my question(s):
          > o is there a better way of passing a comma separated list as the 'b'
          > parameter
          > o is gcc being generous by
          > a) preprocessing this
          > b) compiling it
          > o how can i write a macro like this:
          >
          > #define F(a,b) int a(obj*o b
          >
          > so that i can write function declarations in which all functions have a
          > first param obj, but may optionally have more.
          >
          > thanks,
          >
          >
          > jack
          > ivorykite.com[/color]

          Hello,

          If all the others posibilities fails, you can always use an external
          and more powerful preprocessor, like "m4".

          Kind regards.

          Comment

          • Jordan Abel

            #6
            Re: preprocessor and parenthesis

            On 2006-02-06, effbiae <effbiae@ivoryk ite.com> wrote:[color=blue]
            > hi,
            >
            > this code:
            >
            > #define M(a,b) static int is[3]={a,b;
            > M(1,(2,3)})
            >
            > produces this (using gcc -E):
            >
            > static int is[3]={1,(2,3)};
            >
            > which gcc does not complain about.[/color]

            I believe this statement is equivalent to
            static int is[3] = {1,3,0};

            Comment

            Working...