Skip to content

Instantly share code, notes, and snippets.

@groundwater
Created July 18, 2016 05:29
Show Gist options
  • Save groundwater/3300d8918572e3640964780bfb5ca40b to your computer and use it in GitHub Desktop.
Save groundwater/3300d8918572e3640964780bfb5ca40b to your computer and use it in GitHub Desktop.
#import <stdio.h>
template<int i>
class Integer {
public:
static const int value = i;
};
template<class Left, class Right>
class Add {
public:
static const int value = Left::value + Right::value;
};
template<class Item, class Next>
class List {
public:
using value = Item;
using next = Next;
};
class Nil {};
template<class List>
class head {
public:
using value = typename List::value;
};
template<class List>
class tail {
public:
using value = typename List::next;
};
template<class List>
class sum {
using lhs = typename head<List>::value;
using rhs = typename sum<typename tail<List>::value >::value;
using tot = Add<lhs, rhs>;
public:
using value = Integer<tot::value>;
};
template<>
class sum<Nil> {
public:
using value = Integer<0>;
};
using aa = List<Integer<10>, List<Integer<2>, List<Integer<8>, Nil> > >;
int main() {
printf("%d", sum<aa>::value::value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment