Gennaro Prota <[EMAIL PROTECTED]> writes: > On Fri, 22 Nov 2002 13:48:01 +0200, "Peter Dimov" <[EMAIL PROTECTED]> > wrote: > >>From: "David Abrahams" <[EMAIL PROTECTED]> >>> >>> 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 [EMAIL PROTECTED] * http://www.boost-consulting.com Boost support, enhancements, training, and commercial distribution _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost