Created
September 26, 2015 22:10
-
-
Save ranisalt/5848d0bc9aa3dff23cab to your computer and use it in GitHub Desktop.
C++ variadic sum
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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