Boost logo

Boost :

From: Rene Rivera (grafik666_at_[hidden])
Date: 2002-07-23 18:35:33


[2002-07-23] David Abrahams wrote:

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

Yes! Which is why it would be a bad choice to use it as is in this case :-)
It was only an example of eliminating a template argument by providing a
default typed value.

The safe way is to use the pointer itself, i.e. don't do the *xxx_cast<>(0)
only xxx_cast<>(0) and test the pointer.

But, thanks Dave for pointing out the non-portable aspect.

-- grafik - Don't Assume Anything
-- rrivera_at_[hidden] - grafik_at_[hidden]
-- 102708583_at_icq - Grafik666_at_AIM - Grafik_at_[hidden]


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