Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualCornstalks

Posted 19 March 2013 - 12:21 AM

*snip*

Ah, good catch!
 

I think we should pool our efforts and make a programming language where this kind of thing is sane.

I was hoping for a more concise solution, but here we go (so now you can use commas and hyphens in your numbers!):
#include <type_traits>

template <typename T>
struct Number
{
    mutable T n;
    Number(T n) : n(n) {}

    const Number<T> operator+() const
    {
        return *this;
    }

    template <typename U>
    const Number<typename std::common_type<T, U>::type> operator, (const Number<U>& num) const
    {
        return n + num.n;
    }

    template <typename U>
    const Number<typename std::common_type<T, U>::type> operator- (const Number<U>& num) const
    {
        return n + num.n;
    }

    template <typename U>
    const Number<typename std::common_type<T, U>::type> operator+ (const Number<U>& num) const
    {
        return n + num.n;
    }

    template <typename U>
    const Number<typename std::common_type<T, U>::type> operator* (const Number<U>& num) const
    {
        return n * num.n;
    }

    operator T() const
    {
        return n;
    }
};

Number<unsigned long long> operator"" _n(unsigned long long n)
{
    return n;
}

#define ONE      +1_n
#define TWO      +2_n
#define THREE    +3_n
#define FOUR     +4_n
// ... add more if you wish
#define TWENTY   +20_n
#define HUNDRED  *100_n
#define THOUSAND *1000_n
#define AND

#define TIMES *
#define MINUS -

#include <iostream>

int main()
{
    // The only real drawback is that you have to surround them with parentheses if you use ,
    int x = (FOUR THOUSAND, ONE HUNDRED AND TWENTY-THREE);
    int y = (ONE HUNDRED AND ONE);
    int result = x TIMES y;
    std::cout << x << " * " << y << " = " << result << std::endl;

    result = x MINUS y;
    std::cout << x << " - " << y << " = " << result << std::endl;
}

#1Cornstalks

Posted 19 March 2013 - 12:20 AM

*snip*

Ah, good catch!
 

I think we should pool our efforts and make a programming language where this kind of thing is sane.

I was hoping for a more concise solution, but here we go (so now you can use commas and hyphens in your numbers!):
#include <type_traits>

template <typename T>
struct Number
{
    mutable T n;
    Number(T n) : n(n) {}

    const Number<T> operator+() const
    {
        return *this;
    }

    template <typename U>
    const Number<typename std::common_type<T, U>::type> operator, (const Number<U>& num) const
    {
        return n + num.n;
    }

    template <typename U>
    const Number<typename std::common_type<T, U>::type> operator- (const Number<U>& num) const
    {
        return n + num.n;
    }

    template <typename U>
    const Number<typename std::common_type<T, U>::type> operator+ (const Number<U>& num) const
    {
        return n + num.n;
    }

    template <typename U>
    const Number<typename std::common_type<T, U>::type> operator* (const Number<U>& num) const
    {
        return n * num.n;
    }

    operator T() const
    {
        return n;
    }
};

Number<unsigned long long> operator"" _n(unsigned long long n) // cooked form
{
    return n;
}

#define ONE      +1_n
#define TWO      +2_n
#define THREE    +3_n
#define FOUR     +4_n
// ... add more if you wish
#define TWENTY   +20_n
#define HUNDRED  *100_n
#define THOUSAND *1000_n
#define AND

#define TIMES *
#define MINUS -

#include <iostream>

int main()
{
    // The only real drawback is that you have to surround them with parentheses if you use ,
    int x = (FOUR THOUSAND, ONE HUNDRED AND TWENTY-THREE);
    int y = (ONE HUNDRED AND ONE);
    int result = x TIMES y;
    std::cout << x << " * " << y << " = " << result << std::endl;

    result = x MINUS y;
    std::cout << x << " - " << y << " = " << result << std::endl;
}

PARTNERS