472,099 Members | 2,273 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,099 software developers and data experts.

#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 5983
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
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.