Created
October 4, 2017 19:53
-
-
Save ahupowerdns/39259cc7bcd960ba85539e9e81dc7d1e to your computer and use it in GitHub Desktop.
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 <atomic> | |
#include <utility> | |
#include <functional> | |
/* goal, replace: | |
if(x < 1) | |
++c0_1; | |
else if(x < 10) | |
++c1_10; | |
else if(x< 100) | |
++c10_100; | |
else | |
++cSlow; | |
by: | |
increm(x, 1, c1, 10, c10, 100, c100, cSlow); | |
*/ | |
// for cSlow | |
template<typename T, typename V> | |
void increm(T val, V& counter) | |
{ | |
++counter; | |
} | |
// if we ended up on the last limit/counter pair | |
template<typename T, typename V> | |
void increm(T val, T lim, V& counter) | |
{ | |
if(val < lim) | |
++counter; | |
} | |
// the entry version | |
template<typename T, typename V, typename... Args> | |
void increm(T val, T lim, V& counter, Args... args) | |
{ | |
if(val < lim) | |
++counter; | |
else increm(val, args...); | |
} | |
int main(int argc, char** argv) | |
{ | |
std::atomic<int> c0_1, c1_10, c10_100, cSlow; | |
// this breaks because it does not take a reference to cSlow | |
increm(12, 1, c0_1, cSlow); | |
// v l c l c l c c | |
increm(12, 1, c0_1, 10, c1_10, 100, c10_100, cSlow); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment