|
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