469,927 Members | 1,617 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,927 developers. It's quick & easy.

#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.
Jul 22 '05 #1
3 5833
Bolin wrote:
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?


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
Jul 22 '05 #2
Bolin wrote:
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?


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
Jul 22 '05 #3

"Bolin" <ga*******@voila.fr> wrote in message
news:93*************************@posting.google.co m...
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.


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
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

22 posts views Thread by ineedyourluvin1 | last post: by
7 posts views Thread by AES | last post: by
27 posts views Thread by Peter Ammon | last post: by
3 posts views Thread by Robert Scheer | last post: by
5 posts views Thread by hprYeV | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.