Boost logo

Boost :

From: Jonathan Turkanis (technews_at_[hidden])
Date: 2004-01-23 19:21:36


"Howard Hinnant" <hinnant_at_[hidden]> wrote in message
news:69C9B9AB-4DFD-11D8-9184-003065D18932_at_twcny.rr.com...
> On Jan 23, 2004, at 6:02 PM, Jonathan Turkanis wrote:
>

>
> I think Rani's suggestion is the best way to go. Maybe something
like
> (untested):
>
> template <class T, class U>
> struct is_array_convertible
> {
> private:
> typedef typename remove_bounds<T>::type t_element;
> typedef typename remove_bounds<U>::type u_element;
> typedef typename remove_cv< t_element >::type t_basic;
> typedef typename remove_cv< u_element >::type u_basic;
> public:
> static const bool value =
> is_array<T>::value &&
> is_array<U>::value &&
> is_same<t_basic, u_basic>::value &&
> is_convertible<t_element*, u_element*>::value;
> };
>
> ...
>
> template<typename U>
> move_ptr(
> const move_ptr< U >& ptr,
> typename enable_if
> <
> is_array_convertible<U, T[]>::value
> >::type* = 0);
>

is_array_convertible is certainly more elegant that is_more_cv.
Unfortunately, is_array and remove_bounds don't seem to work on array
types of unknown bounds, so I don't think the above will work as is.
However, that is easily remedied:

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

    template<typename T>
    struct remove_unkown_bounds<T[0]> {
        typedef T type;
    };

    // etc. for is_array

Also, I'm a little nervous about relying on is_convertible, because of
problems like:

     is_convertible<noncopyable,noncopyable>

I'm pretty sure is_array_convertible can be implemented, though.

> You might see messy. I see: Wow, that's really cool! ;-)

I like that attitude. :-)

> As long as we're on the subject, make sure you restrict
move_ptr<U[]>
> conversions to move_ptr<T>, even if is_convertible<U*, T*>:
>
> move_ptr<int> p(move_ptr<int[]>()); // should not compile
>

I've already taken care of this (I think.)

Thanks.

Jonathan


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