Last active
February 27, 2017 16:08
-
-
Save kantoniak/ba0ca485449551a3f5e3ca5b480b9383 to your computer and use it in GitHub Desktop.
Simple variadic template example
This file contains 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> | |
using namespace std; | |
// Support single argument | |
auto sumElements() { | |
return 0; | |
} | |
template <typename T, typename... Ts> | |
T sumElements(T t, Ts... ts) { | |
return t + sumElements(ts...); | |
} | |
int main() { | |
cout << "Example 1: sum elements" << endl; | |
cout << "Sum: " << sumElements(4, 5.f, 6L) << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment