Skip to content

Instantly share code, notes, and snippets.

@neel
Created October 31, 2012 10:00
Show Gist options
  • Save neel/3986190 to your computer and use it in GitHub Desktop.
Save neel/3986190 to your computer and use it in GitHub Desktop.
#include <iostream>
namespace meta{
template <int Begin, int End, bool done = false>
struct iterator{
template<typename F>
static void iterate(F f){
f();
iterator<Begin, End-1, Begin == End-1>::iterate(f);
}
};
template <int Begin, int End>
struct iterator<Begin, End, true>{
template <typename F>
static void iterate(F){}
};
}
void print(){
std::cout << "print !!" << std::endl;
}
int main(){
meta::iterator<4, 7>::iterate(&print);
return 0;
}
#include <iostream>
namespace meta{
template <int Begin, int End, bool done = false>
struct iterator{
template<typename F>
static void iterate(F f){
f<End>();
iterator<Begin, End-1, Begin == End-1>::iterate(f);
}
};
template <int Begin, int End>
struct iterator<Begin, End, true>{
template <typename F>
static void iterate(F){}
};
}
template <int i>
void print(){
std::cout << "print !!" << i << std::endl;
}
int main(){
meta::iterator<4, 7>::iterate(&print);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment