|
Boost : |
From: David Abrahams (dave_at_[hidden])
Date: 2004-03-07 08:35:22
Aleksey Gurtovoy <agurtovoy_at_[hidden]> writes:
> To: C++ extensions mailing list
> Message c++std-ext-6834
>
> David Abrahams writes:
>> "Powell, Gary" <powellg_at_[hidden]> writes:
>> >>From my reading of this paper, it would appear that this change does
>> > that. I also like that the change reuses an existing keyword in what
>> > would appear to be a natural extension of its use. i.e. explicit,
>> > still means explicit, you have to specify not rely on the default.
>>
>> I haven't read the paper yet, but IMO a bigger problem is the fact
>> that we don't have a single syntax for requesting an explicit
>> conversion that will always be "safe". For example:
>>
>> T(x) // if T is a builtin type this might be a
>> // reinterpret_cast
>>
>> static_cast<T>(x) // might be a downcast
>
> That's solvable, though:
>
> template< typename T, typename S >
> T explicit_cast(S const& s, typename enable_if< is_class<T> >::type* = 0 )
> {
> return T(s);
> }
>
> template< typename T, typename S >
> T explicit_cast(S const& s, typename disable_if< is_class<T> >::type* = 0)
> {
> return static_cast<T>(s);
> }
This formulation requires T (and S, if you want to pass rvalues) to
have an accessible copy ctor. I'm not sure it's better than the
formulation in boost/implicit_cast.hpp, which only puts a restriction
on T.
-- Dave Abrahams Boost Consulting www.boost-consulting.com
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk