Alex Rivera | Logout

How do I write functions that accept unlimited arguments?

Asked 2012-01-27T21:57:33.827
33

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 som

Edit
Report

2 Answers

19

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.

answered 2012-01-27T22:10:27.720
7

Instead of passing in the count, you can have a special "trailing" argument (either nullptr or a pointer to some hard-coded "magic" string) and your variable-argument functions should stop extracting more arguments once they see the trailing one. That can ease your coding a bit.

You could also pass pointers (references) to containers, containing (or pointing at/referencing) your strings. Anything that can somehow link all your individual arguments will do (e.g. a vector).

Example (might be not very idiomatic, but should serve as an illustration):

#include <iostream>
#include <string>
#include <cstdarg>
#include <cctype>
#include <vector>

using namespace std;

void AntiCapitalize(vector<string*>& v);
void Capitalize(string* s, ...);
void Print(string* s, ...);

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, 0);
    Capitalize(&s3, &s4, &s5, &s6, 0);
    Capitalize(&s7, &s8, &s9, &s10, &s11, &s12, 0);

    Print(&s1, &s2, 0);
    Print(&s3, &s4, &s5, &s6, 0);
    Print(&s7, &s8, &s9, &s10, &s11, &s12, 0);

    vector<string*> v;
    v.push_back(&s1);
    v.push_back(&s2);
    v.push_back(&s3);
    v.push_back(&s4);
    v.push_back(&s5);
    v.push_back(&s6);
    v.push_back(&s7);
    v.push_back(&s8);
    v.push_back(&s9);
    v.push_back(&s10);
    v.push_back(&s11);
    v.push_back(&s12);

    AntiCapitalize(v);

    Print(&s1, &s2, 0);
    Print(&s3, &s4, &s5, &s6, 0);
    Print(&s7, &s8, &s9, &s10, &s11, &s1
answered 2012-01-27T23:02:48.577

Your Answer