Trent Buck wrote:
> (Note: C99 supports variadic macros, but C89 does not.)
>
> I'm pretty sure what I'm trying to do is impossible, but I'll ask here
> in case I'm missing something.
>
> I'm trying to define generic, variadic arithmetic and boolean operators.
> For example,
>
> product (2, sum (3, 4), 5);
>
> should expand to
>
> (2) * ((3) + (4)) * (5);
>
> Here is my pseudocode:
>
> #define sum(x,y) \
> (x) + (y)
>
> #define sum(x, y, ...) \
> (x) + sum (y, __VA_ARGS__)
>
> int main (void)
> {
> sum (1, 2, 3, 4);
> return 0;
> }
>
> Unfortunately, this has two problems:
>
> - Macros cannot be overriden, hence the base case cannot be caught
> generically. (You can define the base case as a function if the
> definition (and declaration) precede the macro definition.)
>
> - Macros expansion is not recursive (apparently).
My suggestion is to change your design so that you are passing
containers (lists, vectors, arrays, etc.) to each function. Each
function would return the result. This design would eliminate
the need for variable argument lists.
int sum(int * array_of_integers,
unsigned int num_integers)
{
int result;
unsigned int index;
for (index = 0, result = 0;
index < num_integers;
++index)
{
result += *array_of_integers++;
}
return result;
}
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq:
http://www.parashift.com/c++-faq-lite
C Faq:
http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library