Boost logo

Boost :

From: Alexander Nasonov (alnsn_at_[hidden])
Date: 2002-08-31 05:59:17


Douglas Gregor wrote:
> How does this affect the use of any (from a user's point-of-view)? While
> I'd agree that this formulation is easier on the implementation side
> (although potentially less efficient due to the use of multiple
> inheritance), I don't see how it affects the interface to 'any'? Does it
> make 'any' more useful?

Yes, definitely, because exact type matching is not required anymore. for
example:

  struct X { /* ... */ };
  struct Y : X { /* ... */ };

  any y = Y();
  X & rx = any_cast<X &>(y); // doesn't throw!

With new 'any' you can walk through hierarchy like you do using
dynamic_cast. But it's longer distance walk because you can walk through
non-polymorphic hierarchies, accross them and even visit non-class types:

  any a = 0;
  int & ri = any_cast<int &>(a);
  ri = 3; // now 'a' contains int(3)
  a = string();
  int * p = any_cast<int *>(a);
  assert(p == 0); // because 'a' contains string, not int
  a = Y();
  const X & rx = any_cast<const X &>(a);

But new behavior will break existent user code just because users expect
exact matching while new any_cast doesn't require it. If boost::any was
used in the above code fragments then user would expect that
boost::bad_any_cast to be thrown at last line of each fragment.
I think it's better to rename new 'any' class to something like dynany
(analogy with CORBA::DynAny) or dynamic_any. It's more dynamic then
boost::any and it can be reflected in its name.

--
Best regards,
Alexander Nasonov
e-mail account: alnsn
e-mail server:  mail.ru

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