473,772 Members | 2,349 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<typena me 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 3229
sw*****@googlem ail.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<typena me 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*****@google mail.comwrote in message
news:11******** **************@ y42g2000hsy.goo glegroups.com.. .
if have simple macro defined this way:
#define M_OPplus( classname ) typedef classname operator+(const
classname& rhs);

and a template class :
template<typena me 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(classn ame_macro) \
typedef classname_macro () \
operator+(const classname_macro ()& rhs)
template<typena me X, typename Y>
class Test1 {
#define M_Test1() Test1<X,Y>
public:
double run();
M_OPplus(M_Test 1);
};
See what I am getting at here?

Sep 14 '07 #3
"Barry" <dh*****@gmail. comwrote in message news:fc******** **@aioe.org...
sw*****@googlem ail.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(classn ame) \
typedef classname \
operator+(const classname &rhs)

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

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

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

Sep 14 '07 #4
Chris Thomasson wrote:
"Barry" <dh*****@gmail. comwrote in message news:fc******** **@aioe.org...
>sw*****@googlem ail.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(classn ame) \
typedef classname \
operator+(const classname &rhs)

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

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

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

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

and a template class :
template<typen ame 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(classn ame_macro) \
typedef classname_macro () \
operator+(const classname_macro ()& rhs)
template<typena me 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_Test 1);
};
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(classn ame_macro) \
typedef classname_macro () \
operator+(const classname_macro ()& rhs)

template<typen ame 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(classn ame) \
typedef classname \
operator+(const classname &rhs)

template<typena me X, typename Y>
class Test1 {
#define M_Test1 Test1<X,Y>
public:
double run();
M_OPplus(M_Test 1);
};
____________
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(class name) \
typedef classname

#define M_OPplus(classn ame) \
M_Typedef(class name) \
operator+(const classname &rhs)

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

____________

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

#define M_OPplus(classn ame_macro) \
M_Typedef(class name_macro) \
operator+(const classname_macro ()& rhs)

template<typena me X, typename Y>
class Test1 {
#define M_Test1() Test1<X,Y>
public:
double run();
M_OPplus(M_Test 1);
};
____________
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(classn ame_macro) \
typedef classname_macro () \
operator+(const classname_macro ()& rhs)

template<type name 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_FUNC TION(func_ptr)f unc_ptr()

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

int main(void) {
puts(CALL_MACRO _FUNCTION(MY_ME SSAGE));
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(classn ame_macro) \
typedef classname_macro () \
operator+(const classname_macro ()& rhs)

template<type name 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(classn ame) \
typedef classname \
operator+(const classname &rhs)

template<typena me X, typename Y>
class Test1 {
#define M_Test1 Test1<X,Y>
public:
double run();
M_OPplus(M_Test 1);
};
____________
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(class name) \
typedef classname

#define M_OPplus(classn ame) \
M_Typedef(class name) \
operator+(const classname &rhs)

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

____________

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

#define M_OPplus(classn ame_macro) \
M_Typedef(class name_macro) \
operator+(const classname_macro ()& rhs)

template<typena me X, typename Y>
class Test1 {
#define M_Test1() Test1<X,Y>
public:
double run();
M_OPplus(M_Test 1);
};
____________
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

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

Similar topics

699
34232
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 capabilities, unfortunately. I'd like to know if it may be possible to add a powerful macro system to Python, while keeping its amazing syntax, and if it could be possible to add Pythonistic syntax to Lisp or Scheme, while keeping all of the...
27
5929
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 gcc specific. Is there a way to do it in a standard c++ way? TIA
41
2314
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 will do is scour through the file, replacing all macros with global const variables and inline functions. So for instance, if you have: #define MAX_LOADSTRING 100
4
1858
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 #name; } Is there a way to achieve this without macros in C++ ? With templates perhaps ?
6
2811
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 type parameters - in place of a template argument one can construct arbitrary valid C++ type declaration, more or less like in "typedef" statement. What about nontype parameters? Am I right that you cannot partially
9
35461
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 into the queries but on running the macro I have to enter each of the dates 3 times - once for each query. Is there an easy way to ask for the parameters once and for these to be passed onto each query?
19
7937
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 macro: #define min(a,b) ((a<b)?a:b) I thought that another way could be to use a template function: template <class T> T min<T a, T b>
5
6357
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() { THIS_IS_A_MACRO(tracked::TrackedBasicType<int>); // this works
2
2326
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. The reason why I need this: I want a fixed Index->Value mapping that should be part of a compile time API. The straight forward way to do this would be an array, e.g.: const int Mapper = {27, 225, 815, 4711, ..};
0
9621
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10264
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9914
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8937
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5355
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4009
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3610
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.