Skip to content

Instantly share code, notes, and snippets.

@loloof64
Last active September 16, 2016 12:15
Show Gist options
  • Save loloof64/045de9f2b4478a044df07194e5064c90 to your computer and use it in GitHub Desktop.
Save loloof64/045de9f2b4478a044df07194e5064c90 to your computer and use it in GitHub Desktop.
C++ variadic sum doubles
#include <iostream>
#include <cstdarg>
#include <limits>
using namespace std;
double myAdd(double a, double b, ...);
int main()
{
cout << "Summing 10, 1, 2, 3, 4, 5 : " << myAdd(10.0, 1.0, 2.0, 3.0, 4.0, 5.0, numeric_limits<double>::infinity()) << endl;
cout << "Summing 10, 20 : " << myAdd(10.0, 20.0, numeric_limits<double>::infinity()) << endl;
return 0;
}
double myAdd(double a, double b, ...){
double sum{a+b}, current;
va_list remainingValues;
va_start(remainingValues, b);
while ((current = va_arg(remainingValues, double)) != numeric_limits<double>::infinity()){
sum += current;
}
va_end(remainingValues);
return sum;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment