Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write functions that accept unlimited arguments?

I have only been able to find one way for functions to take a variable amount of arguments.
It's this way:

#include <iostream> #include <stdarg.h>  using namespace std;  void Print(int argumentAmount, ... );  int main() {     Print(5,11,22,33,44,55); }  void Print(int argumentAmount, ... ){      va_list arguments;      va_start(arguments, argumentAmount);      int parameter;     for(int i = 0; i < argumentAmount; ++i ){         parameter = va_arg(arguments, int);         cout << parameter << endl;     }      va_end(arguments);     return; } 

2 Problems:
1.) I have to specify how many arguments I'm sending in- not desirable
2.) I can't figure out how to modify it so it will output strings.

Would something like this be possible without having to overload the function multiple times:

void Output(/*not sure how this would look*/);  int main(){      Output("hello","world");     Output("this","is","a","test");     Output("As","many","strings","as","you","want","may","be","passed","in");      return 0; } void Output(/*not sure how this would look*/){      //loop through each string passed in and output it } 

What about this:

void Capitalize(/*all passed by reference*/);  int main(){      string s1 = "hello";     string s2 = "world";      string s3 = "this";     string s4 = "is";     string s5 = "a";     string s6 = "test";      string s7 = "as";     string s8 = "many";     string s9 = "strings";     string s10 = "as";     string s11 = "you";     string s12 = "want";      Capitalize(s1,s2);     Capitalize(s3,s4,s5,s6);     Capitalize(s7,s8,s9,s10,s11,s12);      return 0; } void Capitalize(/*all passed by reference*/){      //capitalize each string passed in  } 

All I can think to do is:
-overload the function multiple times
-have the function accept some type of container instead

If this is NOT POSSIBLE, could someone explain why the compiler is not capable of accomplishing a task like this.

like image 256
Trevor Hickey Avatar asked Jan 27 '12 21:01

Trevor Hickey


People also ask

Can a function take unlimited number of arguments?

When you call a function in JavaScript, you can pass in any number of arguments, regardless of what the function declaration specifies. There is no function parameter limit. In the above function, if we pass any number of arguments, the result is always the same because it will take the first two parameters only.

How do you write a function that accepts any number of arguments?

To make a function that accepts any number of arguments, you can use the * operator and then some variable name when defining your function's arguments.

How many arguments can a function accept?

Except for functions with variable-length argument lists, the number of arguments in a function call must be the same as the number of parameters in the function definition. This number can be zero. The maximum number of arguments (and corresponding parameters) is 253 for a single function.

How do you pass infinite arguments in JavaScript?

Basically, it is an array of parameters, which allows us to pass unlimited arguments in a JavaScript function. We created sum() function using rest parameters syntax. Inside of the function we used Array. reduce() method in combination with arrow functions, to sum an unlimited number of arguments.


2 Answers

With variadic templates in C++11, you can do something like this (see the result at ideone)

#include <string> #include <iostream>  void Output() {     std::cout<<std::endl; }  template<typename First, typename ... Strings> void Output(First arg, const Strings&... rest) {     std::cout<<arg<<" ";     Output(rest...); }  int main() {     Output("I","am","a","sentence");     Output("Let's","try",1,"or",2,"digits");     return 0; } 
like image 144
Alexey Kukanov Avatar answered Oct 08 '22 10:10

Alexey Kukanov


Quick and simple answer.

For C++ you need to specify either the number of arguments or a sentinel value to indicate the end of arguments.

Your first example is a good example of specing the count, you could also do:

void Print(const char *arg, ... ){     va_list arguments;      for (va_start(arguments, arg); arg != NULL; arg = va_arg(arguments, const char *)) {         cout << arg << endl;     }      va_end(arguments); } 

Where your calling convention is:

Print("foo","bar",NULL); 

If you want to take it to the next level, you can mix in a bit of the C Preprocessor and do:

#define mPrint(...) Print(__VA_ARGS__, NULL) 

Now you can just say:

mPrint("fooo","bar"); 

And the macro will NULL terminate the call.

like image 32
koblas Avatar answered Oct 08 '22 12:10

koblas