I was playing around a bit with functions with variable arguments, and decided to make a function to create vectors with the arguments. My function for creating an int vector worked…
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 string vector:
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 crashes the program. What am I doing wrong?
Attempting to pass a string as a varaidic parameter gives undefined behavior: “If the argument has a non-POD class type (clause 9), the behavior is undefined.” (§5.2.2/7 of the standard).