Boost logo

Boost :

From: Valentin Bonnard (Bonnard.V_at_[hidden])
Date: 2000-09-03 13:27:26


On Sun, Sep 03, 2000 at 09:48:21PM +1200, Mark Rodgers wrote:

> Very nice. 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);
> }

Or

any& operator= (any rhs)
{
  return swap (rhs);
}

> 2. Your implementations of const and non-const versions of to_ptr
> are the wrong way round.

Everyone is free to program const-polymorphic code as he wish.

> In the const version, you cast away
> constness leading to undefined behaviour.

Casting away constness never causes undefined behaviour.

It just makes it more easier to get.

> You should put the
> work in the const version and cast away the constness of the
> return value in the non-const version.

Not necessarily.

> 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.

Or check the typeid of the holder instead of the object, to ensure
that the static_cast is valid. By avoiding a call to a virtual function,
you'll also gain some nano secondes.

-- 
Valentin Bonnard

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