Sign in to follow this  
DjMaSh

infinite parameters???

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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%?

Share this post


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

Share this post


Link to post
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 this post


Link to post
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 this post


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

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this