|
Boost Users : |
From: cp now (cppnow_at_[hidden])
Date: 2008-01-30 02:03:28
On Jan 29, 2008 11:03 PM, John Maddock <john_at_[hidden]> wrote:
> cp now wrote:
> >> The type_traits library gives me somewhat erroneous answers when
> >> dealing with pairs of pairs.
>
> >> The compiler appears to be attempting a piecewise conversion of the
> >> contents of the pair, hence the error about converting pair<int,
> >> int> to 'int' rather than an error about converting pair<pair<int,
> >> int>, int> to pair<int, int>.
> >>
> >> Is there a method of converting that is_convertible has in mind when
> >> it returns true for the aforementioned conversion? I'm testing here
> >> for simple implicit conversion...
>
> is_convertible does tell you about implicit convertibility, it answers the
> question:
>
> Does type "To" have a copy constructor that accepts a type "From"?
>
> And indeed std::pair has such a constructor, so is_convertible answers
> true.
>
> Unfortunately what no traits class can answer is the question:
>
> Does this code compile?
>
> And that's where this example fails: std::pair has a "catch all"
> constructor
> that will accept any kind of pair as an argument, so that
> is_convertible<pair1_type, pair2_type>::value must always be true, even
> though the code may fail to compile inside the constructor :-(
>
> However... given that pairs (and in the future tuples) are part of the
> standard, it may be worth while fixing is_convertible for this special
> case.
>
> Interestingly, had std::pair's constructor been defined in terms of
> enable_if and is_convertible, it could have been defined in such a way
> that
> it's signature was only valid if the code would compile: and
> is_convertible
> would then have "just plain worked" for pairs. No doubt C++0x's concepts
> can achieve something similar.
>
> Hope this explanation helps, John.
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>
Makes sense.
Any quick patch possible for is_convertible? I'm thinking if TO and FROM are
either both pairs or both tuples (of the same size) then is_convertible is
just recursively applied to each contained type. I tried to decipher the
header file but it's a little beyond my abilities.
Probably could also eventually be extended to any multi-type container like
spirit containers?
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net