Created
January 7, 2014 16:40
-
-
Save mkmik/8302137 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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