Skip to content

Instantly share code, notes, and snippets.

@U-MA

U-MA/va_list_sample.cpp

Last active Oct 8, 2015
Embed
What would you like to do?
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
You can’t perform that action at this time.