[ create a new paste ] login | about

Link: http://codepad.org/qjN5KmsN    [ raw code | output | fork ]

joel_f - C++, pasted on Jan 6:
#include <iostream>

template<class F, int I, int N, int Step = 1, bool Stop = (Step>0) ? (I < N) : (N < I)>
struct for_
{
  for_() 
 {
    body_(I,N);
    for_<F,I+Step,N,Step> next;
 }

 F body_; 
};

template<class F, int I , int N, int Step> 
struct for_<F,I,N,Step,false> {};

struct display
{
  void operator()(int i,int n) { std::cout << i << std::endl; }
};

int main()
{
  for_<display,0,10>();
  std::cout <<"\n";
  for_<display,0,10,3>();
  std::cout <<"\n";
  for_<display,10,0,-1>();
  std::cout <<"\n";
}


Output:
0
1
2
3
4
5
6
7
8
9

0
3
6
9

10
9
8
7
6
5
4
3
2
1



Create a new paste based on this one


Comments: