Passing templated types as parameter to macro

M

martin.brodeur

I unable to pass a template type with two parameters to a very simple
macro with g++ 3.4 (Linux x86):

for example:

#define THIS_IS_A_MACRO(token) BOOST_PP_STRINGIZE(token)

void foo()
{
THIS_IS_A_MACRO(tracked::TrackedBasicType<int>); // this works

THIS_IS_A_MACRO(tracked::TrackedBasicType2<int,char>); // this
doesn't

// g++ complains this: macro "THIS_IS_A_MACRO" passed 2 arguments,
but takes just 1

}

Is there a way to pass templatized types (with multiple params) to a
macro definition ?

thanks in advance.

MB
 
M

Marcus Kwok

I unable to pass a template type with two parameters to a very simple
macro with g++ 3.4 (Linux x86):

for example:

#define THIS_IS_A_MACRO(token) BOOST_PP_STRINGIZE(token)

void foo()
{
THIS_IS_A_MACRO(tracked::TrackedBasicType<int>); // this works

THIS_IS_A_MACRO(tracked::TrackedBasicType2<int,char>); // this
doesn't

// g++ complains this: macro "THIS_IS_A_MACRO" passed 2 arguments,
but takes just 1

}

Is there a way to pass templatized types (with multiple params) to a
macro definition ?

I'm not sure if it will work, but you could try

#define THIS_IS_A_MACRO(token) BOOST_PP_STRINGIZE((token))

(note the extra set of parentheses around 'token').
 
S

Sylvester Hesp

Marcus Kwok wrote :
I'm not sure if it will work, but you could try

#define THIS_IS_A_MACRO(token) BOOST_PP_STRINGIZE((token))

(note the extra set of parentheses around 'token').

The error is in using THIS_IS_A_MACRO with 2 parameters, while it
expects one. Changing the "implementation" of THIS_IS_A_MACRO won't
change anything.

- Sylvester
 
M

Marcus Kwok

Sylvester Hesp said:
Marcus Kwok wrote :

The error is in using THIS_IS_A_MACRO with 2 parameters, while it
expects one. Changing the "implementation" of THIS_IS_A_MACRO won't
change anything.

What about changing the call site to add the extra parentheses there:

THIS_IS_A_MACRO((tracked::TrackedBasicType2<int,char>));

If that works, then you might be able to get more natural syntax by
adding another level of macro indirection. I recall a similar technique
we used in a C course to wrap printf() calls with a macro that expanded
to another macro, so that lint wouldn't complain when we didn't check
the return value, but the exact details escape me at the moment.
 
M

Marcus Kwok

Marcus Kwok said:
What about changing the call site to add the extra parentheses there:

THIS_IS_A_MACRO((tracked::TrackedBasicType2<int,char>));

If that works, then you might be able to get more natural syntax by
adding another level of macro indirection. I recall a similar technique
we used in a C course to wrap printf() calls with a macro that expanded
to another macro, so that lint wouldn't complain when we didn't check
the return value, but the exact details escape me at the moment.

OK, I was able to find it and I was wrong; the "trick" we used was to
use the double-parentheses so that the variable number of arguments to
printf() could be passed to the second intermediate macro.
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top