|
Boost : |
From: David Abrahams (dave_at_[hidden])
Date: 2002-11-29 13:55:49
Gennaro Prota <gennaro_prota_at_[hidden]> writes:
> 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) )
>
I think I'd prefer to see:
// make sure the static_cast below is a valid expression
BOOST_STATIC_ASSERT(sizeof(implicit_cast<dst_type>(expr)));
... static_cast<dst_type>(expr) ...
I know you have to write expr twice, but it still seems much clearer
to me.
-- David Abrahams dave_at_[hidden] * http://www.boost-consulting.com Boost support, enhancements, training, and commercial distribution
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk