Skip to content

Instantly share code, notes, and snippets.

@kantoniak
Last active February 27, 2017 17:21
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 kantoniak/f53c4e1d83413cc949100ed2a09193cd to your computer and use it in GitHub Desktop.
Save kantoniak/f53c4e1d83413cc949100ed2a09193cd to your computer and use it in GitHub Desktop.
Moving average using variadic templates. Takes average of consecutive 3-number sequence.
#include <iostream>
using namespace std;
template <typename U1, typename U2, typename U3>
void movingAvg(U1 u1, U2 u2, U3 u3) {
cout << "AVG(" << u1 << ", " << u2 << ", " << u3 << ") = " << (u1 + u2 + u3) / 3.f << endl;
}
template <typename U1, typename U2, typename U3, typename... Ts>
void movingAvg(U1 u1, U2 u2, U3 u3, Ts... ts) {
movingAvg(u1, u2, u3);
movingAvg(u2, u3, ts...);
}
int main() {
cout << "Example 2: moving average" << endl;
movingAvg(4, 16, 78, 0, 15, -300, 680);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment