Last active
November 29, 2015 21:36
-
-
Save disconnect3d/9bf4fbb36ffa63bc4e34 to your computer and use it in GitHub Desktop.
C++ template for loop
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; | |
template<int c> | |
struct X | |
{ | |
template<typename F> | |
static auto forEach(F func) | |
{ | |
func(c); | |
X<c-1>::forEach(func); | |
} | |
}; | |
template<> | |
struct X<0> | |
{ | |
template<typename F> | |
static auto forEach(F func) | |
{ | |
func(0); | |
} | |
}; | |
int main() | |
{ | |
auto l = [](int a) { std::cout << "loop counter:" << a << std::endl; }; | |
X<4>::forEach(l); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment