Skip to content

Instantly share code, notes, and snippets.

@gcmurphy
Last active December 6, 2025 07:22
Show Gist options
  • Select an option

  • Save gcmurphy/6217834 to your computer and use it in GitHub Desktop.

Select an option

Save gcmurphy/6217834 to your computer and use it in GitHub Desktop.
Templates for loop unrolling / metaprogramming.
#include <iostream>
#include <cstdint>
template<int i, uint64_t val, typename Function>
class Loop {
public:
static inline void call(Function f){
f(i, val);
Loop<i-1, val, Function>::call(f);
}
};
template<uint64_t val, typename Function>
class Loop<0, val, Function>{
public:
static inline void call(Function f){
f(0, val);
}
};
void print(int i, uint64_t val){
char c = ((val) & (((uint64_t)1) << i)) ? '1' : '0';
std::cout << c ;
}
int main(void){
const uint64_t number = 0x00000001;
Loop<63, number, decltype(print)>::call(print);
std::cout << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment