Boost logo

Boost Users :

From: John Maddock (john_at_[hidden])
Date: 2008-01-29 12:33:09


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 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