Skip to content

Instantly share code, notes, and snippets.

@mkmik
Created January 7, 2014 16:40
Show Gist options
  • Save mkmik/8302137 to your computer and use it in GitHub Desktop.
Save mkmik/8302137 to your computer and use it in GitHub Desktop.
#include <iostream>
template<int n, class T>
struct cons {
static const int value = n;
};
struct nil{};
/* count */
template<class T>
struct cnt {};
template<>
struct cnt<nil> {
static const int value = 0;
};
template<int n, class T>
struct cnt<cons<n, T> > {
static const int value = 1 + cnt<T>::value;
};
/* sum */
template<class T>
struct sum {};
template<>
struct sum<nil> {
static const int value = 0;
};
template<int n, class T>
struct sum<cons<n, T> > {
static const int value = n + sum<T>::value;
};
int main() {
typedef cons<10, cons<20, cons<30, nil> > > lst;
std::cout << "cnt: " << cnt<lst>::value << std::endl;
std::cout << "sum: " << sum<lst>::value << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment