infinite parameters???
Hey, I'm not sure if this is possible but heres what I want to do.
I want to define a function that can (theoretically) take an infinite amount of parameters. Is this possible without overloading a function an infinite amount of times?
For example:
int sum(int p1, int p2, int p3 , ... , int pn){
return p1 + p2 + ... + pn;
}
and then both these calls will call the same 'sum' function:
sum(1, 2, 3);
sum(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
Or something...
Would you need to use reg-exps or something? I dont know anything about them.
I guess you could do that, or you can use a variable number of arguments (not recommended), but would'nt it be easier to use a vector argument?
- Why do you want to do this? 99.9% of the time there are better ways.
- Somebody will doubtless recommend using the ellipsis operator (...) to declare a variable length argument list. In C this might be reasonable. In C++ see 1.
I assume you are using c++.
Doing functions with variable numbers of parameters in c++ is ugly and trickey but possible. What you do is in your function declaration:
n is i.e. your number of terms to sum. The ... is called an ellipsis and it tells the compiler that this function has an unspecified number of arguments.
To access the variables passed to the function you use the type va_list and the macros va_start, va_args and va_end. Check out how on this site.
Now you can call your function by:
Im not sure if you can get around having to pass the number of terms to sum though.
Hope it helps!
//Emil
Doing functions with variable numbers of parameters in c++ is ugly and trickey but possible. What you do is in your function declaration:
int sum(n, ...)
n is i.e. your number of terms to sum. The ... is called an ellipsis and it tells the compiler that this function has an unspecified number of arguments.
To access the variables passed to the function you use the type va_list and the macros va_start, va_args and va_end. Check out how on this site.
Now you can call your function by:
sum(5, 1, 2, 3, 4, 5);
Im not sure if you can get around having to pass the number of terms to sum though.
Hope it helps!
//Emil
I'm pretty sure it's possible, as something like sprintf can take a varying number of arguments. I remember learning how sprintf worked a long time ago but my memory is a little fuzzy... I'll see if I can find my old notes and get back to you.
Quote:Original post by Enigma
Why do you want to do this? 99.9% of the time there are better ways.
I dont really have a purpose for it just yet, I just wanted to know if it is possible. It always comes into my mind and i think it would be handy everynow and then. Besides, what harm can a little more knowledge do?
And what about that .1%?
Quote:Original post by cannonicus
Im not sure if you can get around having to pass the number of terms to sum though.
Once inside the function with variable argument count, you have no way to determine how many arguments were passed. Either you have to send the argument count explicitly as an argument (like in your example), or you have to handle it somehow else (like reading the arguments until some "end" value is found).
In case of sprintf, the argument count is not passed explicitly, but one argument is loaded from the queue for each "%" found in the format string.
Well the overloading is out of the question since we're talking about support for an infinite amount of arguments.
Variable argument list is like eating dinner with a baseball bat.
If you dont like using a vector for the job, I would rather suggest you overload the << operator.
I made this little test so I might as well show it to you.
Variable argument list is like eating dinner with a baseball bat.
If you dont like using a vector for the job, I would rather suggest you overload the << operator.
#include <iostream>template<class digit_type>struct Adder{ Adder() : sum(0) {} digit_type sum; friend Adder& operator << (Adder& adder, const digit_type& digit) { adder.sum += digit; return adder; }};int main(){ Adder<int> adder; adder << 1 << 2 << 3; std::cout << adder.sum << std::endl; return 0;}
I made this little test so I might as well show it to you.
In this particular situation, there is no gain of using sum(x1,...xn) over plain old x1+...xn. In general, an operator-overloading approach, as illustrated above, is also a good option.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement