473,410 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

MACRO and template with two ( or more ) parameters

Hi

if have simple macro defined this way:
#define M_OPplus( classname ) typedef classname operator+(const
classname& rhs);

and a template class :
template<typename X, typename Y>
class Test1
{
public:
double run();
M_OPplus( Test1<X,Y)
};
The compiler (VS2003) is complaining : warning C4002: too many actual
parameters for macro, . I think it is related to the comma in the
middle , the preprocessor may think that i am passing two parameters
to the macro.

Is it possible to fix it and does it conform with the C++ standart?

Many Thx

Sep 14 '07 #1
12 3207
sw*****@googlemail.com wrote:
Hi

if have simple macro defined this way:
#define M_OPplus( classname ) typedef classname operator+(const
classname& rhs);

and a template class :
template<typename X, typename Y>
class Test1
{
public:
double run();
M_OPplus( Test1<X,Y)
^

the comma split up your argument into to parts

write
M_OPplus(( Test1<X,Y))

};
The compiler (VS2003) is complaining : warning C4002: too many actual
parameters for macro, . I think it is related to the comma in the
middle , the preprocessor may think that i am passing two parameters
to the macro.

Is it possible to fix it and does it conform with the C++ standart?

Many Thx

--
Thanks
Barry
Sep 14 '07 #2
<sw*****@googlemail.comwrote in message
news:11**********************@y42g2000hsy.googlegr oups.com...
if have simple macro defined this way:
#define M_OPplus( classname ) typedef classname operator+(const
classname& rhs);

and a template class :
template<typename X, typename Y>
class Test1
{
public:
double run();
M_OPplus( Test1<X,Y)
};
The compiler (VS2003) is complaining : warning C4002: too many actual
parameters for macro, . I think it is related to the comma in the
middle , the preprocessor may think that i am passing two parameters
to the macro.
Your correct.

Is it possible to fix it and does it conform with the C++ standart?
Yes. You can use a little trick that involves passing a name of a macro
function to the M_OPplus macro which calls it to get at the tokens.
Something like this:
#define M_OPplus(classname_macro) \
typedef classname_macro() \
operator+(const classname_macro()& rhs)
template<typename X, typename Y>
class Test1 {
#define M_Test1() Test1<X,Y>
public:
double run();
M_OPplus(M_Test1);
};
See what I am getting at here?

Sep 14 '07 #3
"Barry" <dh*****@gmail.comwrote in message news:fc**********@aioe.org...
sw*****@googlemail.com wrote:
[...]
the comma split up your argument into to parts

write
M_OPplus(( Test1<X,Y))
[...]

That introduces extra parenthesis which can muck up syntax. For instance:
This has a syntax error:
_______________
#define M_OPplus(classname) \
typedef classname \
operator+(const classname &rhs)

template<typename X, typename Y>
class Test1 {
public:
double run();
M_OPplus((Test1<X,Y>));
};
_______________

This does not:
_______________
#define M_OPplus(classname_macro) \
typedef classname_macro() \
operator+(const classname_macro()& rhs)

template<typename X, typename Y>
class Test1 {
#define M_Test1() Test1<X,Y>
public:
double run();
M_OPplus(M_Test1);
};
_______________

Sep 14 '07 #4
Chris Thomasson wrote:
"Barry" <dh*****@gmail.comwrote in message news:fc**********@aioe.org...
>sw*****@googlemail.com wrote:
[...]
>the comma split up your argument into to parts

write
M_OPplus(( Test1<X,Y))

[...]

That introduces extra parenthesis which can muck up syntax. For instance:
This has a syntax error:
_______________
#define M_OPplus(classname) \
typedef classname \
operator+(const classname &rhs)

template<typename X, typename Y>
class Test1 {
public:
double run();
M_OPplus((Test1<X,Y>));
};
_______________

This does not:
_______________
#define M_OPplus(classname_macro) \
typedef classname_macro() \
operator+(const classname_macro()& rhs)

template<typename X, typename Y>
class Test1 {
#define M_Test1() Test1<X,Y>
public:
double run();
M_OPplus(M_Test1);
};
_______________
yeh, you are right,
didn't carefully view the context

--
Thanks
Barry
Sep 14 '07 #5
Chris Thomasson wrote:
<sw*****@googlemail.comwrote in message
news:11**********************@y42g2000hsy.googlegr oups.com...
>if have simple macro defined this way:
#define M_OPplus( classname ) typedef classname operator+(const
classname& rhs);

and a template class :
template<typename X, typename Y>
class Test1
{
public:
double run();
M_OPplus( Test1<X,Y)
};
The compiler (VS2003) is complaining : warning C4002: too many actual
parameters for macro, . I think it is related to the comma in the
middle , the preprocessor may think that i am passing two parameters
to the macro.

Your correct.

>Is it possible to fix it and does it conform with the C++ standart?

Yes. You can use a little trick that involves passing a name of a macro
function to the M_OPplus macro which calls it to get at the tokens.
Something like this:
#define M_OPplus(classname_macro) \
typedef classname_macro() \
operator+(const classname_macro()& rhs)
template<typename X, typename Y>
class Test1 {
#define M_Test1() Test1<X,Y>
#define M_Test1 ....

definition and invocation should be identical
public:
double run();
M_OPplus(M_Test1);
};
See what I am getting at here?

--
Thanks
Barry
Sep 14 '07 #6
"Barry" <dh*****@gmail.comwrote in message news:fc**********@aioe.org...
Chris Thomasson wrote:
[...]
>#define M_OPplus(classname_macro) \
typedef classname_macro() \
operator+(const classname_macro()& rhs)

template<typename X, typename Y>
class Test1 {
#define M_Test1() Test1<X,Y>
#define M_Test1 ....

definition and invocation should be identical
[...]

I am not exactly sure what you mean here. Are you suggesting that 'M_Test1'
does not really need to be a macro function? Well, in that case the code
would need to be modified to something like:

____________
#define M_OPplus(classname) \
typedef classname \
operator+(const classname &rhs)

template<typename X, typename Y>
class Test1 {
#define M_Test1 Test1<X,Y>
public:
double run();
M_OPplus(M_Test1);
};
____________
At first glance I thought that the 'M_Test1' macro would be expanded and
then passed to 'M_OPplus' which would produce the original error that the OP
was asking about. After I compiled it (gcc) I realized that it was getting
expanded in the context of the 'M_OPplus'.

I guess the only drawback from this would be that you could not pass the
'classname' parameter to another similar macro function. For instance:
this does not compile:
____________
#define M_Typedef(classname) \
typedef classname

#define M_OPplus(classname) \
M_Typedef(classname) \
operator+(const classname &rhs)

template<typename X, typename Y>
class Test1 {
#define M_Test1 Test1<X,Y>
public:
double run();
M_OPplus(M_Test1);
};

____________

while this does:
____________
#define M_Typedef(classname_macro) \
typedef classname_macro()

#define M_OPplus(classname_macro) \
M_Typedef(classname_macro) \
operator+(const classname_macro()& rhs)

template<typename X, typename Y>
class Test1 {
#define M_Test1() Test1<X,Y>
public:
double run();
M_OPplus(M_Test1);
};
____________
So I guess defining 'M_Test1' as a macro function instead of a plain macro
is more "flexible".
Any thoughts?

Sep 14 '07 #7
Chris Thomasson wrote:
"Barry" <dh*****@gmail.comwrote in message news:fc**********@aioe.org...
>Chris Thomasson wrote:
[...]
>>#define M_OPplus(classname_macro) \
typedef classname_macro() \
operator+(const classname_macro()& rhs)

template<typename X, typename Y>
class Test1 {
#define M_Test1() Test1<X,Y>
#define M_Test1 ....

definition and invocation should be identical

[...]

I am not exactly sure what you mean here. Are you suggesting that
'M_Test1' does not really need to be a macro function? Well, in that
case the code would need to be modified to something like:

I mean if you
#define M_Test1() ...
then call
M_Test1() other than M_Test1

if you
#define M_Test1 ...
then call
M_Test1 other than M_Test1()

--
Thanks
Barry
Sep 14 '07 #8
"Barry" <dh*****@gmail.comwrote in message news:fc**********@aioe.org...
Chris Thomasson wrote:
>"Barry" <dh*****@gmail.comwrote in message
news:fc**********@aioe.org...
>>Chris Thomasson wrote:
[...]
>I am not exactly sure what you mean here. Are you suggesting that
'M_Test1' does not really need to be a macro function? Well, in that case
the code would need to be modified to something like:
[...]
I mean if you
#define M_Test1() ...
then call
M_Test1() other than M_Test1
Well, as soon as I call M_Test1() it will expand on the spot. I want to be
able to delay expansion until the exact place I need it. Therefore, I can
use the _name_ of the macro function M_Test1 as a sort-of function pointer.
This is the "trick" I mentioned to the OP...
Here is an example program you can compile:
_______________
#include <cstdio>

#define CALL_MACRO_FUNCTION(func_ptr)func_ptr()

#define MY_MESSAGE() "Press <ENTERto exit."

int main(void) {
puts(CALL_MACRO_FUNCTION(MY_MESSAGE));
getchar();
return 0;
}

_______________
As far as I can tell, this is 100% legitimate, and conforms to the standard.

Does that make sense to you?

Sep 14 '07 #9
Chris Thomasson wrote:
"Barry" <dh*****@gmail.comwrote in message news:fc**********@aioe.org...
>Chris Thomasson wrote:
[...]
>>#define M_OPplus(classname_macro) \
typedef classname_macro() \
operator+(const classname_macro()& rhs)

template<typename X, typename Y>
class Test1 {
#define M_Test1() Test1<X,Y>
#define M_Test1 ....

definition and invocation should be identical

[...]

I am not exactly sure what you mean here. Are you suggesting that
'M_Test1' does not really need to be a macro function? Well, in that
case the code would need to be modified to something like:

____________
#define M_OPplus(classname) \
typedef classname \
operator+(const classname &rhs)

template<typename X, typename Y>
class Test1 {
#define M_Test1 Test1<X,Y>
public:
double run();
M_OPplus(M_Test1);
};
____________
At first glance I thought that the 'M_Test1' macro would be expanded and
then passed to 'M_OPplus' which would produce the original error that
the OP was asking about. After I compiled it (gcc) I realized that it
was getting expanded in the context of the 'M_OPplus'.

I guess the only drawback from this would be that you could not pass the
'classname' parameter to another similar macro function. For instance:
this does not compile:
____________
#define M_Typedef(classname) \
typedef classname

#define M_OPplus(classname) \
M_Typedef(classname) \
operator+(const classname &rhs)

template<typename X, typename Y>
class Test1 {
#define M_Test1 Test1<X,Y>
public:
double run();
M_OPplus(M_Test1);
};

____________

while this does:
____________
#define M_Typedef(classname_macro) \
typedef classname_macro()

#define M_OPplus(classname_macro) \
M_Typedef(classname_macro) \
operator+(const classname_macro()& rhs)

template<typename X, typename Y>
class Test1 {
#define M_Test1() Test1<X,Y>
public:
double run();
M_OPplus(M_Test1);
};
____________
So I guess defining 'M_Test1' as a macro function instead of a plain
macro is more "flexible".

Well, forgive my English

You know

#define SOME_MACRO()

int main()
{
SOME_MACRO();
SOME_MACRO; // does not compile
}

so IMHO,
the definition and invoking of macro should better give the same form.

--
Thanks
Barry
Sep 14 '07 #10
"Barry" <dh*****@gmail.comwrote in message news:fc**********@aioe.org...
Chris Thomasson wrote:
[...]
You know

#define SOME_MACRO()

int main()
{
SOME_MACRO();
SOME_MACRO; // does not compile
}
[...]

Your correct that it does not compile. However this does compile:

____________
#define CALL_IT(func_ptr)func_ptr()
#define SOME_MACRO()

int main(){
SOME_MACRO();
CALL_IT(SOME_MACRO);
return 0;
}

____________
See what I am getting at here?
Sep 14 '07 #11
Chris Thomasson wrote:
"Barry" <dh*****@gmail.comwrote in message news:fc**********@aioe.org...
>Chris Thomasson wrote:
[...]
>You know

#define SOME_MACRO()

int main()
{
SOME_MACRO();
SOME_MACRO; // does not compile
}
[...]

Your correct that it does not compile. However this does compile:

____________
#define CALL_IT(func_ptr)func_ptr()
#define SOME_MACRO()

int main(){
SOME_MACRO();
CALL_IT(SOME_MACRO);
return 0;
}

____________
See what I am getting at here?

I think I have to learn more about macro first.
thanks for your patience

--
Thanks
Barry
Sep 14 '07 #12
So do i,
thx anyway , i am going to test these tricks

Sep 14 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
27
by: TheDD | last post by:
Hello all, right now, i'm using the following macro to automatically add informations to exceptions: #define THROW(Class, args...) throw Class(__FILE__, __LINE__, ## args) but AFAIK, it's...
41
by: JKop | last post by:
I was doing some Win32 programming today, having to include the file "windows.h". Anyway, I'm thinking of writing a program that'll work like so: macrodestroyer.exe windows.h What this program...
4
by: jjleto | last post by:
I have a C program that uses in some parts macros with # and ## like in: #define GET_FUNC_DECL(name) char *get##name(); #define GET_FUNC_IMPL(name) char *get##name() { /* some stuff */; return...
6
by: rincewind | last post by:
Hi, can anybody summarise all options for partial template specialization, for all kind of parameters (type, nontype, template)? I *think* I understand options for partial specialization on...
9
by: skinnybloke | last post by:
Hi - I have 3 access queries which I run via 1 macro. Each of the queries now requires 2 parameters when they the run. The parameters are start and end dates. I have built the parameters...
19
by: aaragon | last post by:
Hi everyone. A very simple question. I would like to know what is better in terms of performance. I want to use a simple function to obtain the minimum of two values. One way could be using a...
5
by: martin.brodeur | last post by:
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() {...
2
by: Christof Warlich | last post by:
Hi macro experts, in a variadic macro, i.e. in a macro with a variable parameter list, is there any way to access single parameters of the list? __VA_ARGS__ only expands to the whole list. ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.