Boost logo

Boost :

From: bill_kempf (williamkempf_at_[hidden])
Date: 2002-01-16 11:55:22


--- In boost_at_y..., Mac Murrett <mmurrett_at_v...> wrote:
> On 1/16/02 2:20 AM, "jk_at_s..." <jk_at_s...> wrote:
>
> > 16 Jan 2002 06:52:11 +0300 Mac Murrett ???????:
> >> Its purpose is to convert anything to anything, and it looks
like this:
> >>
> >> // force_cast will convert anything to anything.
> >>
> >> template<class Return_Type, class Argument_Type>
> >> inline Return_Type force_cast(const Argument_Type &rSrc)
> >> { return(*reinterpret_cast<const Return_Type *>
(&rSrc)); }
> >
> > Then copying will occur. Wouldn't it be better to return by
reference,
> > like below?
> >
> > template<class Return_Type, class Argument_Type>
> > inline const Return_Type& force_cast(const Argument_Type &rSrc)
> > { return(*reinterpret_cast<const Return_Type *>(&rSrc)); }
>
> If a user wants this, (s)he can use force_cast<Type &>. Also, I
don't think
> that returning a const & by default is fair, as users should have
the right
> to modify their data :).

I didn't notice this in your original code. A cast does only that,
it casts the type. When you use such a cast you should still be
manipulating the same memory space: you should only be treating it as
some new type.

Bill Kempf


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