Skip to content

Instantly share code, notes, and snippets.

@ranisalt
Created September 26, 2015 22:10
Show Gist options
  • Save ranisalt/5848d0bc9aa3dff23cab to your computer and use it in GitHub Desktop.
Save ranisalt/5848d0bc9aa3dff23cab to your computer and use it in GitHub Desktop.
C++ variadic sum
#include <iostream>
template<typename T>
int sum(T v) {
return v;
}
template<typename T, typename... Types>
int sum(T v, Types&&... others) {
return v + sum(others...);
}
int main() {
int a = sum(5, 7, 9);
int b = sum(2);
int c = sum(42, 14, 2, 18, 9);
std::cout << a << ' ' << b << ' ' << c << '\n';
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment