Skip to content

Instantly share code, notes, and snippets.

@U-MA
Last active October 8, 2015 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save U-MA/010a73c84b36d4f1bd29 to your computer and use it in GitHub Desktop.
Save U-MA/010a73c84b36d4f1bd29 to your computer and use it in GitHub Desktop.
va_listのサンプル
#include <cstdio>
#include <cstdarg>
template<typename T>
T sumN(unsigned int n, ...)
{
va_list vl;
va_start(vl, n);
T sum = 0;
for (unsigned int i=0; i < n; ++i)
sum += va_arg(vl, T);
va_end(vl);
return sum;
}
int main()
{
printf("%d\n", sumN<int>(4, 3, 5, 8, 9)); // 25
printf("%g\n", sumN<double>(3, 0.4, 2.4, 6.1)); // 8.9
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment