Using a function with variable argument strings

I played a little with functions with variable arguments and decided to create a function to create vectors with arguments. My vector creation function intworked ...

vector<int> makeIntVector(int numArgs, ...) {
    va_list listPointer;
    va_start(listPointer, numArgs);
    vector<int> made;
    for(int a = 0; a < numArgs; a++)
        made.push_back(va_arg(listPointer, int));
    va_end(listPointer);
    return made;
}

but not my function for creating a vector string:

vector<string> makeStringVector(int numArgs, string something, ...) {
    va_list listPointer;
    va_start(listPointer, something);
    vector<string> made;
    for(int a = 0; a < numArgs; a++)
        made.push_back(va_arg(listPointer, string));
    va_end(listPointer);
    return made;
}

which causes the program to crash. What am I doing wrong?

+2
source share
3 answers

Attempting to pass a string as a varaidic parameter gives undefined behavior: "If the argument is of a non-POD class type (section 9), the behavior is undefined." (§5.2.2 / 7 of the standard).

+4
source

++.

, POD, int char *, -POD ++ undefined.

, , ?

+1

, , va_* - , int - "" , string - . , -.

EDIT: g++ : -POD 'struct std::string '...;

0

All Articles