I’ve got the class member:
LineND::LineND(double a ...)
{
coefficients.push_back(a);
va_list arguments;
va_start(arguments, a);
double argValue;
do
{
argValue = va_arg(arguments, double);
coefficients.push_back(argValue);
}while(argValue != NULL); // THIS IS A PROBLEM POINT!
va_end(arguments);
}
I don’t know how many arguments will be used. I need to take each argument and put it into the vector called coefficients. How should I do that? I understand, that the statement while(argValue != NULL) is not correct in this case. I can’t use for example this signature:
LineND::LineND(int numArgs, double a ...)
to change the condition like this:
while(argValue != numArgs);
The point is I can’t change the signature of the method. Need to resolve this problem another way.
Variable argument lists have several drawbacks:
Compared to variadic templates:
Example:
You can put this into the private section of a class declaration or in some detail namespace. Note:
pass_me_floats_impl()doesn’t have to be implemented in a header.Then here’s the nice stuff for your client:
He now can do:
But he can’t do:
because that would emit a compile error inside your pass_me_floats-function.
If you need at least, say, 2 arguments, then make it so:
And of course, if you want it a complete inline function, you can also