Skip to content

Instantly share code, notes, and snippets.

@disconnect3d
Last active November 29, 2015 21:36
Show Gist options
  • Save disconnect3d/9bf4fbb36ffa63bc4e34 to your computer and use it in GitHub Desktop.
Save disconnect3d/9bf4fbb36ffa63bc4e34 to your computer and use it in GitHub Desktop.
C++ template for loop
#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