#define and commas

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

    #1

    #define and commas

    I am using a macro to define template functions that look the same
    except for a type. However when the return type it a template class
    with several template arguments (say, A<T1, T2>) then the macro does
    not work because of the comma -- the macro thinks there are more
    arguments provided since the comma separates arguments. To avoid the
    comma being taken into account the usual way is to put parenthesis,
    but here it would not work because the argument is a return type, and
    I cannot declare a function like

    (A<T1, T2>) foo();

    Is there a way to get around this problem?

    Thanks

    B.
  • Victor Bazarov

    #2
    Re: #define and commas

    Bolin wrote:[color=blue]
    > I am using a macro to define template functions that look the same
    > except for a type. However when the return type it a template class
    > with several template arguments (say, A<T1, T2>) then the macro does
    > not work because of the comma -- the macro thinks there are more
    > arguments provided since the comma separates arguments. To avoid the
    > comma being taken into account the usual way is to put parenthesis,
    > but here it would not work because the argument is a return type, and
    > I cannot declare a function like
    >
    > (A<T1, T2>) foo();
    >
    > Is there a way to get around this problem?[/color]

    So, you have something like

    #define MYFUNC(T) A<T> foo##T(T t) { A<T> tt(t); return tt }

    and want to use it to define a function with more than one template
    argument? I guess I don't understand. Post more code.

    Alternative: define a different macro for more than one argument:

    #define MYFUNC1(T) ...
    #define MYFUNC2(T,U) ...

    and so on.

    V

    Comment

    • chris

      #3
      Re: #define and commas

      Bolin wrote:[color=blue]
      > I am using a macro to define template functions that look the same
      > except for a type. However when the return type it a template class
      > with several template arguments (say, A<T1, T2>) then the macro does
      > not work because of the comma -- the macro thinks there are more
      > arguments provided since the comma separates arguments. To avoid the
      > comma being taken into account the usual way is to put parenthesis,
      > but here it would not work because the argument is a return type, and
      > I cannot declare a function like
      >
      > (A<T1, T2>) foo();
      >
      > Is there a way to get around this problem?
      >[/color]

      The only way of fixing this is to declare multiple macros, one for each
      number of commands in the input.

      The is fixed in C99 with variable length macros, for example by:

      #define DEBUGLIST(...) fprintf(stderr, __VA_ARGS__)

      While this isn't in C++ yet, some compilers (in particular g++) will let
      you use this C99 construct in C++ code, and I would be highly suprised
      if it doesn't end up appearing in the very next version of C++.


      Chris

      Comment

      • John Harrison

        #4
        Re: #define and commas


        "Bolin" <gao_bolin@voil a.fr> wrote in message
        news:93c5215b.0 410291249.47381 63@posting.goog le.com...[color=blue]
        >I am using a macro to define template functions that look the same
        > except for a type. However when the return type it a template class
        > with several template arguments (say, A<T1, T2>) then the macro does
        > not work because of the comma -- the macro thinks there are more
        > arguments provided since the comma separates arguments. To avoid the
        > comma being taken into account the usual way is to put parenthesis,
        > but here it would not work because the argument is a return type, and
        > I cannot declare a function like
        >
        > (A<T1, T2>) foo();
        >
        > Is there a way to get around this problem?
        >
        > Thanks
        >
        > B.[/color]

        None that springs to mind. But the usual way to define functions (templates
        or not) that look the same except for a type is to use a template. So
        perhaps if you post the macro someone could suggest a way to make a template
        from it.

        john


        Comment

        Working...