Podcast #128: We chat with Kent C Dodds about why he loves React and discuss what life was like in the dark days before Git. Listen now.

Return to Answer

2 Added doge

C++ - Peano numbers with template metaprogramming (with optional doge)

C, like many other programming languages complicate things with absolute no reason. One of the most overcomplex systems in these languages are natural numbers. C is obsessed with the binary representation and all other completely useless details.

In the end, Natural number is just a Zero, or some other natural number incremented by one. These so called Peano numbers are a nice way to represent numbers and do calculation.

If you like doge I have written an C++ extension to allow the use of natural language for programming. The extension and this following code using my extension can be found at: http://pastebin.com/sZS8V8tN

#include <cstdio>

struct Zero { enum { value = 0 }; };

template<class T>
struct Succ { enum { value = T::value+1 }; };

template <unsigned int N, class P=Zero> struct MkPeano;
template <class P>
struct MkPeano<0, P> { typedef P peano; };
template <unsigned int N, class P>
struct MkPeano { typedef typename MkPeano<N-1, Succ<P> >::peano peano; };

template <class T, class U> struct Add;
template <class T>
struct Add<T, Zero> { typedef T result; };
template <class T, class U>
struct Add<T, Succ<U> > { typedef typename Add<Succ<T>, U>::result result; };

main()
{
printf("%d\n", MkPeano<0>::peano::value );
printf("%d\n", MkPeano<1>::peano::value );

printf("%d\n", Add< MkPeano<14>::peano, MkPeano<17>::peano >::result::value );
printf("%d\n", Add< MkPeano<14>::peano, Add< MkPeano<3>::peano, MkPeano<5>::peano>::result >::result::value );
}


To further add the superiority of this method: The math is done at compile time! No more slow programs, your user doesn't want to wait for you to sum those numbers.

And for the serious part:

• I don't think I have to say this, but this is completely ridiculous.
• Works only for compile time constants.
• Doesn't work with negative numbers.
• The answer was provided by a person who actually cannot template metaprogram himself, so I wouldn't even know if it has other flaws.

My friends told me to dogify the code, so I did. It's fun, but I think it takes too much away from the fact that this is totally stupid as it is, so I only included it as a link.

C++ - Peano numbers with template metaprogramming

C, like many other programming languages complicate things with absolute no reason. One of the most overcomplex systems in these languages are natural numbers. C is obsessed with the binary representation and all other completely useless details.

In the end, Natural number is just a Zero, or some other natural number incremented by one. These so called Peano numbers are a nice way to represent numbers and do calculation.

#include <cstdio>

struct Zero { enum { value = 0 }; };

template<class T>
struct Succ { enum { value = T::value+1 }; };

template <unsigned int N, class P=Zero> struct MkPeano;
template <class P>
struct MkPeano<0, P> { typedef P peano; };
template <unsigned int N, class P>
struct MkPeano { typedef typename MkPeano<N-1, Succ<P> >::peano peano; };

template <class T, class U> struct Add;
template <class T>
struct Add<T, Zero> { typedef T result; };
template <class T, class U>
struct Add<T, Succ<U> > { typedef typename Add<Succ<T>, U>::result result; };

main()
{
printf("%d\n", MkPeano<0>::peano::value );
printf("%d\n", MkPeano<1>::peano::value );

printf("%d\n", Add< MkPeano<14>::peano, MkPeano<17>::peano >::result::value );
printf("%d\n", Add< MkPeano<14>::peano, Add< MkPeano<3>::peano, MkPeano<5>::peano>::result >::result::value );
}


To further add the superiority of this method: The math is done at compile time! No more slow programs, your user doesn't want to wait for you to sum those numbers.

And for the serious part:

• I don't think I have to say this, but this is completely ridiculous.
• Works only for compile time constants.
• Doesn't work with negative numbers.
• The answer was provided by a person who actually cannot template metaprogram himself, so I wouldn't even know if it has other flaws.

C++ - Peano numbers with template metaprogramming (with optional doge)

C, like many other programming languages complicate things with absolute no reason. One of the most overcomplex systems in these languages are natural numbers. C is obsessed with the binary representation and all other completely useless details.

In the end, Natural number is just a Zero, or some other natural number incremented by one. These so called Peano numbers are a nice way to represent numbers and do calculation.

If you like doge I have written an C++ extension to allow the use of natural language for programming. The extension and this following code using my extension can be found at: http://pastebin.com/sZS8V8tN

#include <cstdio>

struct Zero { enum { value = 0 }; };

template<class T>
struct Succ { enum { value = T::value+1 }; };

template <unsigned int N, class P=Zero> struct MkPeano;
template <class P>
struct MkPeano<0, P> { typedef P peano; };
template <unsigned int N, class P>
struct MkPeano { typedef typename MkPeano<N-1, Succ<P> >::peano peano; };

template <class T, class U> struct Add;
template <class T>
struct Add<T, Zero> { typedef T result; };
template <class T, class U>
struct Add<T, Succ<U> > { typedef typename Add<Succ<T>, U>::result result; };

main()
{
printf("%d\n", MkPeano<0>::peano::value );
printf("%d\n", MkPeano<1>::peano::value );

printf("%d\n", Add< MkPeano<14>::peano, MkPeano<17>::peano >::result::value );
printf("%d\n", Add< MkPeano<14>::peano, Add< MkPeano<3>::peano, MkPeano<5>::peano>::result >::result::value );
}


To further add the superiority of this method: The math is done at compile time! No more slow programs, your user doesn't want to wait for you to sum those numbers.

And for the serious part:

• I don't think I have to say this, but this is completely ridiculous.
• Works only for compile time constants.
• Doesn't work with negative numbers.
• The answer was provided by a person who actually cannot template metaprogram himself, so I wouldn't even know if it has other flaws.

My friends told me to dogify the code, so I did. It's fun, but I think it takes too much away from the fact that this is totally stupid as it is, so I only included it as a link.

1

C++ - Peano numbers with template metaprogramming

C, like many other programming languages complicate things with absolute no reason. One of the most overcomplex systems in these languages are natural numbers. C is obsessed with the binary representation and all other completely useless details.

In the end, Natural number is just a Zero, or some other natural number incremented by one. These so called Peano numbers are a nice way to represent numbers and do calculation.

#include <cstdio>

struct Zero { enum { value = 0 }; };

template<class T>
struct Succ { enum { value = T::value+1 }; };

template <unsigned int N, class P=Zero> struct MkPeano;
template <class P>
struct MkPeano<0, P> { typedef P peano; };
template <unsigned int N, class P>
struct MkPeano { typedef typename MkPeano<N-1, Succ<P> >::peano peano; };

template <class T, class U> struct Add;
template <class T>
struct Add<T, Zero> { typedef T result; };
template <class T, class U>
struct Add<T, Succ<U> > { typedef typename Add<Succ<T>, U>::result result; };

main()
{
printf("%d\n", MkPeano<0>::peano::value );
printf("%d\n", MkPeano<1>::peano::value );

printf("%d\n", Add< MkPeano<14>::peano, MkPeano<17>::peano >::result::value );
printf("%d\n", Add< MkPeano<14>::peano, Add< MkPeano<3>::peano, MkPeano<5>::peano>::result >::result::value );
}


To further add the superiority of this method: The math is done at compile time! No more slow programs, your user doesn't want to wait for you to sum those numbers.

And for the serious part:

• I don't think I have to say this, but this is completely ridiculous.
• Works only for compile time constants.
• Doesn't work with negative numbers.
• The answer was provided by a person who actually cannot template metaprogram himself, so I wouldn't even know if it has other flaws.