473,387 Members | 1,573 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,387 software developers and data experts.

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 1457
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...@comAcast.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...@gmail.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...@gmail.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*******@gmail.comwrites:
On Jun 25, 6:16 pm, Victor Bazarov <v.Abaza...@comAcast.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
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...
2
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,...
6
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 : .......
12
by: Sweety | last post by:
plz reply, thanks in advance. bye,
27
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...
7
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...
0
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...
2
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...
3
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.