By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,838 Members | 2,222 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,838 IT Pros & Developers. It's quick & easy.

how to pass variable arg list to a function pointer

P: n/a
hello,

imagine you have a static class method that receives a function
pointer, an int with the number of arguments and a variable number of
arguments.

in that static method you want to call that function (using its
pointer) and call it with the same argument list that was passed into
the static method.

sorry the bad explanation, maybe the code will make it clearer:

#include <iostream>
#include <stdarg.h>

//just a function to sum a variable number of integers
int Sum(int num, ...)
{
int res = 0;
va_list arguments;
va_start(arguments, num);

for(int i = 0; i < num; i++)
{
res += va_arg(arguments, int);
}
return res;
}

//just a function to sub a variable number of integers
int Sub(int num, ...)
{
int res = 0;
va_list arguments;
va_start(arguments, num);

for(int i = 0; i < num; i++)
{
res -= va_arg(arguments, int);
}
return res;
}

class Operation
{
public:
static int Calc(int (*op)(int num, ...), int num, ...)
{
//here is where the problem exists, how can i pass that argument
list to the op function?
return (*op)(num, ...);//ofc this gives me a syntax error.
}
};

int main(int argc, char *argv[])
{
//usage example
std::cout << Operation::Calc(Sum, 3, 1, 2, 3) << std::endl;//should
print 6 (1+2+3)
return 0;
}

thanks in advance :)

Aug 1 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a

ca*************@gmail.com wrote:
//just a function to sub a variable number of integers
int Sub(int num, ...)
{
int res = 0;
va_list arguments;
va_start(arguments, num);

for(int i = 0; i < num; i++)
{
res -= va_arg(arguments, int);
}
return res;
}
just a small EDIT, 'cause the function wasnt working like it should be,
its not really important to the problem it self, but ok.
correct way:

int Sub(int num, ...)
{
va_list arguments;
va_start(arguments, num);
int res = va_arg(arguments, int);

for(int i = 1; i < num; i++)
{
res -= va_arg(arguments, int);
}
return res;
}

Aug 1 '06 #2

P: n/a
ca*************@gmail.com wrote:
imagine you have a static class method that receives a function
pointer, an int with the number of arguments and a variable number of
arguments.

in that static method you want to call that function (using its
pointer) and call it with the same argument list that was passed into
the static method.

[..]
There is no way. You need to implement your 'Sum' and 'Sub' to take
'va_list' instead.

Find an implementation of "fprintf" + "vfprintf". The former takes the
ellipsis argument, the latter takes a 'va_list'. The former calls the
latter. You need to follow that pattern.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 1 '06 #3

P: n/a
ca*************@gmail.com wrote:
hello,

imagine you have a static class method that receives a function
pointer, an int with the number of arguments and a variable number of
arguments.

in that static method you want to call that function (using its
pointer) and call it with the same argument list that was passed into
the static method.

sorry the bad explanation, maybe the code will make it clearer:

#include <iostream>
#include <stdarg.h>

//just a function to sum a variable number of integers
int Sum(int num, ...)
{
int res = 0;
va_list arguments;
va_start(arguments, num);

for(int i = 0; i < num; i++)
{
res += va_arg(arguments, int);
}
return res;
}

//just a function to sub a variable number of integers
int Sub(int num, ...)
{
int res = 0;
va_list arguments;
va_start(arguments, num);

for(int i = 0; i < num; i++)
{
res -= va_arg(arguments, int);
}
return res;
}

class Operation
{
public:
static int Calc(int (*op)(int num, ...), int num, ...)
{
//here is where the problem exists, how can i pass that argument
list to the op function?
return (*op)(num, ...);//ofc this gives me a syntax error.
}
};

int main(int argc, char *argv[])
{
//usage example
std::cout << Operation::Calc(Sum, 3, 1, 2, 3) << std::endl;//should
print 6 (1+2+3)
return 0;
}

thanks in advance :)

Mmmhhh ... I remember reading somewhere in this list, not a long time
ago that it was not possible with the current C++ standard ...

I think the common work-around is to use a macro that defines the
function with different number of parameters. You can have a look at
Boost.Function (http://www.boost.org/doc/html/function.html). But the
idea is to have in the end something like that:

template <class R>
R calc( R(*op)() )
{
return op();
}

template <class R, class Arg1>
R calc( R (*op)(Arg1), Arg1 value )
{
return op(value);
}

template <class R, class Arg1, class Arg2>
R calc( R (*op)(Arg1,Arg2), Arg1 value1, Arg2 value2)
{
return op(value1, value2);
}

....
But IRC, it is a bit more complex if you want to handle reference types
(in that case, better use Boost.Function !!)

Pierre
Aug 1 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.