Boost logo

Boost :

From: C. Green (postmast.root.admi.gov_at_[hidden])
Date: 1999-11-05 15:10:00


At 05:36 PM +0100 1999.11.05, Valentin Bonnard wrote:
>C. Green wrote:
>>
>> At 10:15 AM -0500 1999.11.05, Beman Dawes wrote:
>> >Darin Adler wrote:
>> >
>> [Darin's comments on implicit conversions in implicit_cast]
[snip]
>> >Anyone have a resolution that solves all three issues?
>>
>> template<typename T>
>> struct mirror_type {
>> typedef T type;
>> };
>>
>> template<typename T>
>> inline T implicit_cast( typename mirror_type<T>::type t ) {
>> return t;
>> }
>>
>> Will this work?
>
>Oups ! yes !

Both of our identical solutions probably crossed paths somewhere on the
net, as we seem to have sent them almost at the same moment :-)

>> Side comment, implicit_cast seems to break code that casts to a const
>>ref type:
>>
>> int const& ir = 1.0; // OK - temp int lives as long as ir lives
>> int j = ir; // OK
>>
>> int const& ir2 = implicit_cast<int const&> (1.0); // temp destroyed at
>> EOS, right?
>
>Yes
>
>> int j2 = ir2; // undefined
>
>Yes
>
>> This can be safe if no temp is generated, but how do we know that one is
>> not being generated? (user-defined conversions, built-in conversion like
>> above)
>
>If implicit_cast cannot be used on references types, it just
>isn't a general purpose implicit cast anymore and shouldn't
>be called implicit_cast.

I thought more about this, and (I believe) it's no different than static_cast:

    int const& ir = static_cast<int const&> (1.0);

    int j = ir; // undefined?

I think implicit_cast should behave as closely as possible to static_cast,
except that it only does implicit conversions (and not downcasts, as
static_cast does).

So, I suppose the above behavior is acceptable.


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