Last active
February 27, 2020 07:42
-
-
Save shouth/88d228cf0c445ff0d17a91a33fe68820 to your computer and use it in GitHub Desktop.
This file contains 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<type_traits> | |
#include<iostream> | |
struct nat {}; | |
template<typename T> | |
using is_nat = std::enable_if_t<std::is_base_of<nat, T>::value>; | |
template<typename N, typename = is_nat<N>> | |
struct suc : nat {}; | |
struct _0 : nat {}; | |
using _1 = suc<_0>; | |
using _2 = suc<_1>; | |
using _3 = suc<_2>; | |
using _4 = suc<_3>; | |
using _5 = suc<_4>; | |
using _6 = suc<_5>; | |
using _7 = suc<_6>; | |
using _8 = suc<_7>; | |
using _9 = suc<_8>; | |
using _10 = suc<_9>; | |
template<typename N, typename M, typename = is_nat<N>, typename = is_nat<M>> | |
struct add {}; | |
template<typename N> | |
struct add<N, _0> { using type = N; }; | |
template<typename N, typename M> | |
struct add<N, suc<M>> { using type = typename add<suc<N>, M>::type; }; | |
template<typename N, typename = is_nat<N>> | |
struct to_int {}; | |
template<> | |
struct to_int<_0> { constexpr static int value = 0; }; | |
template<typename N> | |
struct to_int<suc<N>> { constexpr static int value = 1 + to_int<N>::value; }; | |
int main() { | |
std::cout << to_int<add<_3, _7>::type>::value << std::endl; | |
std::cout << to_int<add<_5, _10>::type>::value << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment