-
-
Save crazyguitar/4457149d58218c75c83c9a792bcf938f to your computer and use it in GitHub Desktop.
Templates for loop unrolling / metaprogramming.
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> | |
| #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