macro that defines functions

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2008
Posts: 119
Reputation: kux is on a distinguished road 
Solved Threads: 10
kux kux is offline Offline
Junior Poster

macro that defines functions

 
0
  #1
Feb 19th, 2008
hello,

does anyone know if I can write a macro that takes a number as argument and according to that number declares a variable number of functions ?
ex:

DEFINE_FUNC( 4 );

is replaced with

void func_1();
void func_2();
void func_3();
void func_4();

thx
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,952
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 306
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: macro that defines functions

 
0
  #2
Feb 19th, 2008
As far as I know, this isn't posssible. You could use a class with a memberfunction and then delcare an array of classes. Example:

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class foo
  5. {
  6. public:
  7. int func(int a)
  8. {
  9. return a;
  10. }
  11. };
  12.  
  13. int main(void)
  14. {
  15. const int SIZE = 5;
  16. foo bar[SIZE];
  17.  
  18. for (int i = 0; i < SIZE; i++)
  19. cout << bar[i].func(i) << endl;
  20.  
  21. cin.get();
  22. return 0;
  23. }
Why would you want to do that anyway? Do the functions have the same implementation?
Perhaps you can clarify yourself with an example or code?

Niek
Last edited by niek_e; Feb 19th, 2008 at 6:04 am. Reason: Example code
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 119
Reputation: kux is on a distinguished road 
Solved Threads: 10
kux kux is offline Offline
Junior Poster

Re: macro that defines functions

 
0
  #3
Feb 19th, 2008
ok, thx 4 the help
managed to work arround without the help of that macro
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: macro that defines functions

 
0
  #4
Feb 19th, 2008
Actually, it is possible. You need to use token concatenation.
#define DEFINE_FUNC( n ) void func_ ## n()

You would use it in the normal way:
DEFINE_FUNC( 12 );

The preprocessor would turn that into:
void func_12();


While I can't imagine why you want to do this, there are, in fact, legitimate reasons to do token concatenation (which goes a long way into explaining why the preprocessor can do it at all).

Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,433
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 249
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: macro that defines functions

 
0
  #5
Feb 19th, 2008
Originally Posted by Duoas View Post
Actually, it is possible. You need to use token concatenation.
#define DEFINE_FUNC( n ) void func_ ## n()
How do you propose to do the precompiler looping?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 119
Reputation: kux is on a distinguished road 
Solved Threads: 10
kux kux is offline Offline
Junior Poster

Re: macro that defines functions

 
1
  #6
Feb 19th, 2008
Originally Posted by Duoas View Post
Actually, it is possible. You need to use token concatenation.
#define DEFINE_FUNC( n ) void func_ ## n()

You would use it in the normal way:
DEFINE_FUNC( 12 );

The preprocessor would turn that into:
void func_12();


While I can't imagine why you want to do this, there are, in fact, legitimate reasons to do token concatenation (which goes a long way into explaining why the preprocessor can do it at all).

Hope this helps.
true, what i meant to say is that if u have

DEFINE_FUNC (12)
the preprocessor should replace with 12 function declarations named func_1 func_2 ... func_12
so as Dave said, the looping is the problem

anyway as I said, I managed to carry out my stuff without using this, but if someone can figure out how to do the macro thingie I would be thankfull. I heard something that boost's preprocessor library can do that. didn't get to try it out : http://boost.org/libs/preprocessor/doc/ref/repeat.html.

cheers
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: macro that defines functions

 
0
  #7
Feb 19th, 2008
Ah, I misunderstood the original post.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: macro that defines functions

 
1
  #8
Feb 20th, 2008
> I heard something that boost's preprocessor library can do that.
> didn't get to try it out : http://boost.org/libs/preprocessor/doc/ref/repeat.html.
BOOST_PP_REPEAT gives a fast horizontal repetition from 0 to N-1
since you need a repetition starting at 1, using BOOST_PP_REPEAT_FROM_TO would be easier.
  1. #include <boost/preprocessor/cat.hpp>
  2. #include <boost/preprocessor/repetition/repeat_from_to.hpp>
  3. #include <iostream>
  4.  
  5. #define DEFUN( z, N, arg ) int BOOST_PP_CAT( arg, N )() \
  6. { return N * N ; }
  7.  
  8. #define DEFINE_FUNC(N) \
  9. BOOST_PP_REPEAT_FROM_TO( 1, N, DEFUN, square_of_ )
  10.  
  11. DEFINE_FUNC(15)
  12.  
  13. int main()
  14. {
  15. std::cout << square_of_11() << '\n' ;
  16. }
Last edited by vijayan121; Feb 20th, 2008 at 8:53 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC