Boost logo

Boost :

From: Gennaro Prota (gennaro_prota_at_[hidden])
Date: 2002-11-23 14:13:15


On Fri, 22 Nov 2002 13:48:01 +0200, "Peter Dimov" <pdimov_at_[hidden]>
wrote:

>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; }

I do know we all hate macros but this could be taken into
consideration if we want something usable in constant expressions too.
The idea is to use a static_cast after checking that the conversion
could have also been done implicitly:

template <typename T>
struct identity { typedef T type;};

template <typename T>
T implicit_cast (typename identity<T>::type x) {
    return x;
}

template <typename T, std::size_t sz >
struct check_helper{
  typedef T type;
};

#define IMPLICIT_CAST(dst_type, expr) \
          ( static_cast< check_helper<dst_type, \
            sizeof(implicit_cast<dst_type>(expr)) > \
            :: type>(expr) )

Thoughts?

Genny.


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