473,765 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to write a macro like this?

Hi,

I want a macro #expand that can be expanded to one of the following
depending on its argument

f(i1)
f(i1, i2)
f(i1, i2, i3)

...
For example,

expand(1) would give me f(i1)

expand(2) would give me f(i1, i2)

expand(3) would give me f(i1, i2, i3)

Is it possible to be done in macro? If it is possible, could you let
me know how to do it?

Thanks,
Peng
Jun 27 '08 #1
5 1472
Peng Yu wrote:
I want a macro #expand that can be expanded to one of the following
depending on its argument

f(i1)
f(i1, i2)
f(i1, i2, i3)

..
For example,

expand(1) would give me f(i1)

expand(2) would give me f(i1, i2)

expand(3) would give me f(i1, i2, i3)

Is it possible to be done in macro? If it is possible, could you let
me know how to do it?
//--------------------- here you go
#define with1args f(i1)
#define with2args f(i1,i2)
#define with3args f(i1,i2,i3)
#define formNname(i) with##i##args
#define expand(i) formNname(i)
//---------------- test code
#include <iostream>
#include <ostream>

void f(int) { std::cout << "f(int)\n"; }
void f(int,int) { std::cout << "f(int,int) \n"; }
void f(int,int,int) { std::cout << "f(int,int,int) \n"; }

int main()
{
int i1=0, i2=0, i3=0;
expand(1);
expand(2);
expand(3);
}

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #2
On Jun 25, 6:16 pm, Victor Bazarov <v.Abaza...@com Acast.netwrote:
Peng Yu wrote:
I want a macro #expand that can be expanded to one of the following
depending on its argument
f(i1)
f(i1, i2)
f(i1, i2, i3)
..
For example,
expand(1) would give me f(i1)
expand(2) would give me f(i1, i2)
expand(3) would give me f(i1, i2, i3)
Is it possible to be done in macro? If it is possible, could you let
me know how to do it?

//--------------------- here you go
#define with1args f(i1)
#define with2args f(i1,i2)
#define with3args f(i1,i2,i3)
#define formNname(i) with##i##args
#define expand(i) formNname(i)

//---------------- test code
#include <iostream>
#include <ostream>

void f(int) { std::cout << "f(int)\n"; }
void f(int,int) { std::cout << "f(int,int) \n"; }
void f(int,int,int) { std::cout << "f(int,int,int) \n"; }

int main()
{
int i1=0, i2=0, i3=0;
expand(1);
expand(2);
expand(3);

}

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
I think the example in my OP did not reflect the question that I
wanted to ask.

Suppose that the definitions of f(int), f(int, int) , f(int, int,
int) ... in reality are very complex. However, the difference between
them are minor, except the things that depend on different arguments.
In this case, I would not want to write each definition individually.

Also, in the code snippet that you showed, if the user needs f of 10
arguments, I have to define it ahead of time. But since I can not
enumerate all the possibilities, what if the user want an f of 100 or
1000 arguments.

I suspect that my request can not be done in macro. I looked up the
code of boost tuple, which offers functions and classes of different
number of arguments. It did not use macro in implementation in this
aspect. I just want to double check if my inception is correct.

Thanks,
Peng
Jun 27 '08 #3
On Jun 25, 7:45 pm, r...@zedat.fu-berlin.de (Stefan Ram) wrote:
Peng Yu <PengYu...@gmai l.comwrites:
Suppose that the definitions of f(int), f(int, int) , f(int, int,
int) ... in reality are very complex.

It is usually recommended to refactor a complex function
definition until it is not so complex anymore.
However, the difference between
them are minor, except the things that depend on different arguments.
In this case, I would not want to write each definition individually.

One might use a single array or vector as an argument or
write f as a a function with a variable number of arguments.
It might be OK to certain cases, but it is not suitable to the cases
in my project.

Thanks,
Peng
Jun 27 '08 #4
On Jun 26, 1:51*am, Peng Yu <PengYu...@gmai l.comwrote:
<snip>
I suspect that my request can not be done in macro. I looked up the
code of boost tuple, which offers functions and classes of different
number of arguments. It did not use macro in implementation in this
aspect. I just want to double check if my inception is correct.
Check the boost preprocessor library.

--
gpd
Jun 27 '08 #5
Peng Yu <Pe*******@gmai l.comwrites:
On Jun 25, 6:16 pm, Victor Bazarov <v.Abaza...@com Acast.netwrote:
>Peng Yu wrote:
I want a macro #expand that can be expanded to one of the following
depending on its argument
f(i1)
f(i1, i2)
f(i1, i2, i3)
..
For example,
expand(1) would give me f(i1)
expand(2) would give me f(i1, i2)
expand(3) would give me f(i1, i2, i3)
Is it possible to be done in macro? If it is possible, could you let
me know how to do it?

//--------------------- here you go
#define with1args f(i1)
#define with2args f(i1,i2)
#define with3args f(i1,i2,i3)
#define formNname(i) with##i##args
#define expand(i) formNname(i)

//---------------- test code
#include <iostream>
#include <ostream>

void f(int) { std::cout << "f(int)\n"; }
void f(int,int) { std::cout << "f(int,int) \n"; }
void f(int,int,int) { std::cout << "f(int,int,int) \n"; }

int main()
{
int i1=0, i2=0, i3=0;
expand(1);
expand(2);
expand(3);

}

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

I think the example in my OP did not reflect the question that I
wanted to ask.

Suppose that the definitions of f(int), f(int, int) , f(int, int,
int) ... in reality are very complex. However, the difference between
them are minor, except the things that depend on different arguments.
In this case, I would not want to write each definition individually.

Also, in the code snippet that you showed, if the user needs f of 10
arguments, I have to define it ahead of time. But since I can not
enumerate all the possibilities, what if the user want an f of 100 or
1000 arguments.

I suspect that my request can not be done in macro.
It can be done, almost. You only need an additionnal cpp loop.

http://www.ioccc.org/2001/herrmann1.hint
http://www.ioccc.org/2001/herrmann1.c
http://www.ioccc.org/2001/herrmann1.sh
http://www.ioccc.org/2001/herrmann1.gcd
http://www.ioccc.org/2001/herrmann1.times2

I looked up the
code of boost tuple, which offers functions and classes of different
number of arguments. It did not use macro in implementation in this
aspect. I just want to double check if my inception is correct.
You could probably solve your problem with templates (and boost::mpl),
but I don't think it would be any simplier than with C macros.

What you are really longing for is merely lisp:

(defmacro call-f-with-arguments (n)
`(f ,@(loop :for i :from 1 :to n
:collect (intern (format nil "I~A" i)))))

(macroexpand '(call-f-with-arguments 7))
--(F I1 I2 I3 I4 I5 I6 I7)
Or with local macros:

(defun main ()
(let ((i1 0) (i2 0) (i3 0))
(macrolet ((call-f (n)
`(f ,@(loop :for i :from 1 :to n
:collect (intern (format nil "I~A" i))))))
(call-f 3)))
0)

--
__Pascal Bourguignon__
Jun 27 '08 #6

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

Similar topics

0
1067
by: Garibaldi | last post by:
Folks. This began today. I have many macros. Selecting a macro in the macro explorer then either double clicking on the macro or right clicking and selecting run no text is auto-typed into the IDE code window. IOW, macros don't work for some reason. BTW, the cursor changes to "Run" the returns to normal as if the macro did function properly.
2
3397
by: Edgar | last post by:
I want to go from viewing a form (record number "x") to the same record ("x") in a report. Of course, when I open the report, it opens to the first record, not record "x". I don't know VBA, only macros. Is there any way to do this with macros (I tried time stamping a field in the form when I enter a record and then using a query to sort by max date as
6
6725
by: Clément Collin | last post by:
I working on a GIS project, with Access link which just need a little routine in VBA, but I haven't knowledges in VBA language. It's very simple, and it looks like that in a TPascal way : .... Var RecordNb : integer ; .... {Command : function of Access}
12
4081
by: Sweety | last post by:
plz reply, thanks in advance. bye,
27
5690
by: Sune | last post by:
Hi! Pre-requisites: ------------------- 1) Consider I'm about to write a quite large program. Say 500 K lines. 2) Part of this code will consist of 50 structs with, say, no more than at most 1K bytes of data. 3) These structs are to be used by all of the other 500K lines in various places.
7
23550
by: fei.liu | last post by:
#define ONCFILE_ERR1(funcname, name) \ { \ #ifdef DEBUG\ cerr << __FILE__ << ":" << __LINE__ << " duplicated call to " << funcname << " " << name << endl; \ #endif \ } I want to have conditional macros inside a macro. When I compile this
0
1122
by: Oenone | last post by:
I have a VS2005 macro that runs in my BuildEvents.OnBuildDone event handler each time I build my solution. I'd like to get this macro to write to either the Output/Debug window or the Immediate window in the main VS2005 IDE. My attempts at doing this using Debug.WriteLine have been ignored, the text I'm writing just seems to disappear without trace. Can anyone point me in the right direction please?
2
3505
by: raghunadhs | last post by:
Hi All, i have some simple scripts( which may have some insert statements, update statements etc). these scripts were written in Sql server 2000. now i want to use the same scripts in Access. (since the data is same in both Access, Sql server databases). can i write the scripts in Access-2003? if so please help me. Actually i tried with simple examples. it is working fine for insert, update statements. but it is not working for "If" state...
3
1475
by: thinktwice | last post by:
i want to define a macro like #define STRING2(x) #x #define STRING(x) STRING2(x) #define PROMPT(x) #pragma message(__FILE__ "(" STRING(__LINE__) "):" STRING(x)) <---- compile failed i want to use this macro to remind me that something need to be done but not done yet. is it possible to write such a macro??
0
9568
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
10160
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...
1
9951
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9832
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
8831
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
5275
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
5421
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
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
3531
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.