Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2002-11-29 14:20:14


From: "David Abrahams" <dave_at_[hidden]>
> "Peter Dimov" <pdimov_at_[hidden]> writes:
>
> > From: "David Abrahams" <dave_at_[hidden]>
> >>
> >> Here's what I think might be a correct implementation:
> >>
> >> template <class T, class U> T implicit_cast(U const& x) { return
x; }
> >> template <class T, class U> T implicit_cast(U& x) { return x; }
> >
> > The correct implementation IIRC is
> >
> > template<class T> T implicit_cast(typename identity<T>::type x) { return
> > x; }
>
>
> 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. Here's a test case:

class X
{
};

class Y: private X
{
public:

    void f()
    {
        X * p1 = this;
        X * p2 = implicit_cast<X*>(this);
    }
};

I believe that Darin is the original inventor of this implicit_cast.


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