Boost logo

Boost :

From: Kevlin Henney (kevlin_at_[hidden])
Date: 2000-09-03 06:26:56


In message <002b01c0158c$31c6f0e0$fe9560cb_at_yogi>, Mark Rodgers
<mark.rodgers_at_[hidden]> writes
>Very nice.

Thanks.

>I did notice a couple of little things:
>
>1. Your operator= binds a non-const reference to a temporary in
> the call to swap. Please change to
>
> any & operator=(const any & rhs)
> {
> any temp(rhs);
> return swap(temp);
> }

Oops! Well spotted and now fixed, thanks. Slack compiler and slack
author :->

>2. Your implementations of const and non-const versions of to_ptr
> are the wrong way round. In the const version, you cast away
> constness leading to undefined behaviour. You should put the
> work in the const version and cast away the constness of the
> return value in the non-const version.

They are fine the current way around and the behaviour is perfectly well
defined: You are not modifying a const object.

>3. I have a question mark over the implementation of to_ptr. If
> I write
>
> boost::any a(3);
> const int *pi = a.to_ptr<const int>();
>
> then it seems to me that I'll be casting content to
> holder<const int>, even though it's actually a holder<int>.
> Perhaps you need to use remove_const from type_traits
> in your cast.

It already works for this case. The type check is performed using the
result of typeid rather than dynamic_cast: dynamic_cast<holder<const
int> *> would fail but typeid(int) == typeid(const int) is true, so it
works as it stands -- no deconsting through traits is required.
____________________________________________________________

  Kevlin Henney phone: +44 117 942 2990
  Curbralan Ltd mobile: +44 7801 073 508
  kevlin_at_[hidden] fax: +44 870 052 2289
____________________________________________________________


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