Created
June 20, 2014 16:07
-
-
Save lnicola/c3a811a70bef2defdb1f to your computer and use it in GitHub Desktop.
Peano numerals with C++ templates
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 "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