Boost logo

Boost :

From: David Abrahams (david.abrahams_at_[hidden])
Date: 2002-07-23 17:54:24


From: "Rene Rivera" <grafik666_at_[hidden]>

> [2002-07-23] Craig Henderson wrote:
>
> >>
> >> There's a trick I use to get rid of the need for "extra" template
> >parameters
> >> that might be usefull here...
> >>
> >> template<typename Target, typename Source, typename E>
> >> inline Target lexical_cast(Source arg, E& interpreter =
> >> (*reinterpret_cast<E*>(0))
> >> ...
> >
> >
> >I'm not sure that dereferencing a null pointer in this way to create a
> >reference this is a good idea. Not least, my test on MSVC does not
compile:
> >
> > short &t = *(reinterpret_cast<short*>(0));
> >
> >generates an error
> > error C2040: 't' : 'short &' differs in levels of indirection from
> >'short'
>
> Not usually a good idea unless you have tight control of it's use. It's
too
> bad MSVC chokes on it though :-(

Let me be clear about this: Applying * to a NULL pointer invokes undefined
behavior.

That means that *xxxx_cast<T*>(0) is wrong for any T and any xxxxx, at
least in portable code.

-Dave


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