Skip to content

Instantly share code, notes, and snippets.

@Luiz-Monad
Created January 8, 2016 21:58
Show Gist options
  • Save Luiz-Monad/ee6017d9617045cf98c4 to your computer and use it in GitHub Desktop.
Save Luiz-Monad/ee6017d9617045cf98c4 to your computer and use it in GitHub Desktop.
Peano numbers with constexpr metaprogramming (yet another implementation)
template<typename T>
struct S {
typedef T Previous;
enum { ToInt = T::ToInt + 1 };
};
struct Z { enum { ToInt = 0 }; };
template<typename T>
struct ToType {
typedef T TypeValue;
};
template<>
struct ToType<Z> {
typedef Z TypeValue;
};
template<typename T>
struct ToType<S<T>> {
typedef S<typename ToType<T>::TypeValue> TypeValue;
};
#define P(n) n::Previous
#define TO_INT(n) n::ToInt
int k = TO_INT(P( S < S < Z > > ));
int main(int argc, char** argv) {
int i = k;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment