#define and commas

B

Bolin

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.
 
V

Victor Bazarov

Bolin said:
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
 
C

chris

Bolin said:
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
 
J

John Harrison

Bolin said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top