Boost logo

Boost Users :

From: Mailing List (maillist_at_[hidden])
Date: 2004-05-05 16:18:13


Greetings all, as I'm new to this list, I'll keep my query short.

It was suggested to me to use boost::any to store numeric values of whatever
type (int, float, double, etc) so that I could use one std::list to store
multiple values.

I've looked through the header (any.hpp) and I admit it's a little hard for
me to follow. I'm not what you'd call a superprogrammer... yet. :)

I understand how to store a value in the boost::any object, but I'm not
clear on how one would get that value back later (for display on screen, etc).

What I'd really like to do is similar to one of the examples:

--- snip ---

struct property
{
    property();
    property(const std::string &, const boost::any &);

    std::string name;
    boost::any value;
};

typedef std::list<property> properties;

--- snip ---

But I want to be able to do arithmetic with the resultant object(s):

property T;
property V;
property Z;

T [+-*/] Z;
T = V [+-*/] Z
T + [int,float,etc];

And so on.

However, I want to make sure that the normal arithmetic type conversions
(type promotions) take place (i.e. int + float returns a temporary object
of type float).

Example:

boost::any value1;
boost::any value2;
boost::any value3;

value1 = 127.915;
value2 = 17;
value3 = value1.getwhatevervalue() + value2.getwhatevervalue();

cout << value3.getwhatevervalue() << endl;

value3 = value2.getwhatevervalue() + value1.getwhatevervalue();

cout << value3.getwhatevervalue() << endl;

Both should return a temporary float with a value of 144.915.

since I don't know how to get the value from boost::any, I just used
getwhatevervalue() as an example.

Sincerely,

GRE


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net