Boost logo

Boost :

Subject: Re: [boost] [any] New feature idea for boost.any
From: OvermindDL1 (overminddl1_at_[hidden])
Date: 2009-12-28 21:30:46


On Mon, Dec 28, 2009 at 5:50 PM, Andres B. <andres.b.dev_at_[hidden]> wrote:
> Hello, i am Andrés, i am new in the boost world and i have an idea to
> boost.any.
>
> With the actual boost::any i can't do this:
>
> boost::any any_var;
> int i;
> any_var = 10;
> i = 1 + any_var;
> double d;
> // d = 1.0 + any_var; // throw: bad_any_cast, because any_var is int
> any_var = 10.0;
> d = 1.0 + any_var - 3.0; // now any_var is double
>
> because are incompatible types. To solve this problem, we need add the
> casting operator and add overload the global operators.
>
> I wrote the code and actually have the global operators overloaded +, -, *,
> /, %.
> I don't know how write code to Boost, for this reason maybe has errors.
>
> namespase boost {
>    class any {
>    //....
>    public:
>        // casting operators
>        template <class ValueType>
>        operator ValueType
>        () const
>        {
>            return boost::any_cast<ValueType>(*this);
>        }
>
>        template <class ValueType>
>        operator ValueType
>        ()
>        {
>            return boost::any_cast<ValueType>(*this);
>        }
>        // arithmetic operators
>        template <typename ValueType>
>        ValueType operator+
>        (const ValueType& t1) const
>        {
>            return (ValueType)(*this) + t1;
>        }
>
>        template <typename ValueType>
>        ValueType operator-
>        (const ValueType& t1) const
>        {
>            return (ValueType)(*this) - t1;
>        }
>
>        template <typename ValueType>
>        ValueType operator*
>        (const ValueType& t1) const
>        {
>            return (ValueType)(*this) * t1;
>        }
>
>        template <typename ValueType>
>        ValueType operator/
>        (const ValueType& t1) const
>        {
>            return (ValueType)(*this) / t1;
>        }
>
>        template <typename ValueType>
>        ValueType operator%
>        (const ValueType& t1) const
>        {
>            return (ValueType)(*this) % t1;
>        }
>
>    //...
>
>    } // End any
>
>    //...
>
>    // Global operators
>    template <class ValueType>
>    ValueType operator+
>    (ValueType t1,  boost::any t2)
>    {
>        return t1 + (ValueType)(t2);
>    }
>
>    template <class ValueType>
>    ValueType operator-
>    (ValueType t1,  boost::any t2)
>    {
>        return t1 - (ValueType)(t2);
>    }
>
>    template <class ValueType>
>    ValueType operator*
>    (ValueType t1,  boost::any t2)
>    {
>        return t1 * (ValueType)(t2);
>    }
>
>    template <class ValueType>
>    ValueType operator/
>    (ValueType t1,  boost::any t2)
>    {
>        return t1 - (ValueType)(t2);
>    }
>
>    template <class ValueType>
>    ValueType operator%
>    (ValueType t1,  boost::any t2)
>    {
>        return t1 % (ValueType)(t2);
>    }
> }
>
> If you like it or not, please tell me.

I say no personally, because it requires you to know the type of the
any anyway, and if you know the type then just extract it and have the
fill abilities anyway while being faster (no extra type_id's for each
expansion). I see no benefit to doing it like the above.

A good example for another reason why not using your above modifications:
boost::any any_var;
any_var = (char)42;
auto res = any_var * 2; // fails at runtime since the type in any_var
is a char, not an int, hence you *have* to know the type at compile
time anyway, better to just extract it like you are supposed to.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk