Boost logo

Boost Users :

From: Jim.Hyslop (jim.hyslop_at_[hidden])
Date: 2002-08-27 08:48:18


> How do I compare two boost::any values for equality? How to I compare
> two containers of boost::any's for equality?
>
> Why doesn't boost::any define this operator? There has to be
> a reason.
There is.

boost::any can, quite literally, hold *anything*. In order to compare the
objects being held, you must first know if they _can_ be compared. Consider:

const char * initialMessage = "Snowflakes keep falling on my head";
void f()
{
 boost::any multiA(42L);
 boost::any multiB(initialMessage);
//...

It doesn't make sense to compare the two 'any' objects. I suppose boost::any
could coerce the pointer into a long, and then apply a comparison operator,
but the results of that comparison would be meaningless.

In order to compare the objects, you'll have to extract them from the any
container using an any_cast. Which, of course, means you'll have to know the
exact type of the object being held (or which type of a known set of types).

-- 
Jim

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