Skip to content

Instantly share code, notes, and snippets.

@lnicola
Created June 20, 2014 16:07
Show Gist options
  • Save lnicola/c3a811a70bef2defdb1f to your computer and use it in GitHub Desktop.
Save lnicola/c3a811a70bef2defdb1f to your computer and use it in GitHub Desktop.
Peano numerals with C++ templates
#include "stdafx.h"
class z { };
template<typename>
class s { };
template<typename, typename>
class add { };
template<typename n>
struct add<z, n>
{
typedef n type;
};
template<typename m, typename n>
struct add<s<m>, n>
{
typedef typename add<m, s<n>>::type type;
};
template<typename>
struct eval { };
template<>
struct eval<z>
{
static const int value = 0;
};
template<typename n>
struct eval<s<n>>
{
static const int value = 1 + eval<n>::value;
};
int main()
{
printf("%d\n", eval<add<s<s<z>>, s<s<s<z>>>>::type>::value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment