943,806 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2643
  • C++ RSS
Feb 19th, 2008
0

macro that defines functions

Expand Post »
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
Similar Threads
kux
Reputation Points: 66
Solved Threads: 11
Junior Poster
kux is offline Offline
119 posts
since Jan 2008
Feb 19th, 2008
0

Re: macro that defines functions

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:

C++ Syntax (Toggle Plain Text)
  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 Nick Evan; Feb 19th, 2008 at 6:04 am. Reason: Example code
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Feb 19th, 2008
0

Re: macro that defines functions

ok, thx 4 the help
managed to work arround without the help of that macro
kux
Reputation Points: 66
Solved Threads: 11
Junior Poster
kux is offline Offline
119 posts
since Jan 2008
Feb 19th, 2008
0

Re: macro that defines functions

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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Feb 19th, 2008
0

Re: macro that defines functions

Click to Expand / Collapse  Quote originally posted by Duoas ...
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?
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Feb 19th, 2008
1

Re: macro that defines functions

Click to Expand / Collapse  Quote originally posted by Duoas ...
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
kux
Reputation Points: 66
Solved Threads: 11
Junior Poster
kux is offline Offline
119 posts
since Jan 2008
Feb 19th, 2008
0

Re: macro that defines functions

Ah, I misunderstood the original post.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Feb 20th, 2008
1

Re: macro that defines functions

> 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.
c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++ sequential file / array
Next Thread in C++ Forum Timeline: Need help (newbie)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC