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

Recommended Answers

All 7 Replies

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:

#include <iostream>
using namespace std;

class foo
{
public:
    int func(int a)
    {
       return a;
     }
};

int main(void)
{
    const int SIZE = 5;
    foo bar[SIZE];
	
    for (int i = 0; i < SIZE; i++)
       cout << bar[i].func(i) << endl;

    cin.get();
   return 0;
}

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

ok, thx 4 the help
managed to work arround without the help of that macro

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.

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?

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

commented: Boost never ceases to continue to amaze me. +13

Ah, I misunderstood the original post.

> 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.

#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/repetition/repeat_from_to.hpp>
#include <iostream>

#define DEFUN( z, N, arg ) int BOOST_PP_CAT( arg, N )() \
{ return N * N ; }

#define DEFINE_FUNC(N) \
BOOST_PP_REPEAT_FROM_TO( 1, N, DEFUN, square_of_ )

DEFINE_FUNC(15)
 
int main()
{
  std::cout << square_of_11() << '\n' ;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.