Boost logo

Boost :

From: Gennaro Prota (gennaro_prota_at_[hidden])
Date: 2002-11-30 07:00:23


On Fri, 29 Nov 2002 21:20:14 +0200, "Peter Dimov" <pdimov_at_[hidden]>
wrote:

>From: "David Abrahams" <dave_at_[hidden]>

>> Now I have to put on my inference hat.
>>
>> ...so the use of identity<> assures that we have a non-deduced
>> context, which causes the explicit template parameter to be required?
>>
>> ...I suppose that T has to be copyable for any of these to work, so
>> there's no problem with taking T by-value.
>
>The main reason to prefer this implementation is that it works in contexts
>where the conversion is accessible at the point of the implicit_cast call,
>but not accessible to the definition of implicit_cast.

Yes, that's the reason for doing conversion directly "on the
argument". But I think David was asking why not:

    template<class T>
        T implicit_cast(T x) { return x; }

The reason for this, I think, is that type deduction is based on the
static type of the expression used as argument, so the above would
slice the object passed e.g. in:

 Derived d;
 Base * p = &d;
 implicit_cast(*p); // deduction: T = base

The identity<> trick avoids this possible oversight (well, you must
explicitly specify 'Base' instead of 'Base&' as destination type to
make that error! :-). Maybe there are other reasons too, I asked this
to you in another post. Are there?

Genny.


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