infinite parameters???

This topic is 4086 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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.

Share on other sites
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?

Share on other sites
1. Why do you want to do this? 99.9% of the time there are better ways.

2. 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.
Σnigma

Share on other sites
You might want to look at this.

Share on other sites
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:

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

Share on other sites
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.

Share on other sites
Quote:
 Original post by EnigmaWhy 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?

Share on other sites
Quote:
 Original post by cannonicusIm 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.

Share on other sites
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.
#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.

Share on other sites
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.

Share on other sites
Quote:
Original post by DjMaSh
Quote:
 Original post by EnigmaWhy 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?

OK. It just sounded from your original post that you wanted a solution to a specific problem. Of course it's useful to know about things like variable length argument lists. My warning was simply based on Hammer and Nail syndrome ("When your only tool is a hammer, every problem looks like a nail."). People who learn about variable argument lists have a tendency to then go and apply them in lots of situations. They seem neat at first, but have lots of subtle problems that can bite you quite hard.

Σnigma