Boost logo

Boost Users :

From: cp now (cppnow_at_[hidden])
Date: 2008-01-29 09:08:19


The type_traits library gives me somewhat erroneous answers when dealing
with pairs of pairs.

The following code produces the errors below, but interestingly, the
BOOST_ASSERT actually succeeds. It seems is_convertible thinks a
pair<pair<int, int>, int> is convertible to a plain old pair<int, int>, but
the compiler (and this coder) takes a different view...

#include <utility>
#include <boost/assert.hpp>
#include <boost/type_traits/is_convertible.hpp>

std::pair<int, int> go(std::pair<int, int> p) {
   return p;
}

int main()
{
   BOOST_STATIC_ASSERT((
       boost::is_convertible<
           std::pair<
               std::pair<int, int>,
               int
>,
           std::pair<int, int>
>::value
   ));

   std::pair<std::pair<int, int>, int> p1 =
       std::make_pair(std::make_pair(1,2),3);
   std::pair<int, int> p2 = go(p1);

   return 0;
}

$ g++-4.1.1 test.cpp -I/usr/include/boost-1_33_1
/home/vsekhar/gcc-4.1.1/lib/gcc/i686-pc-cygwin/4.1.1/../../../../include/c++/4.1.1/bits/stl_pair.h:
In constructor 'std::pair<_T1, _T2>::pair(const std::pair<_U1, _U2>&) [with
_U1 = std::pair<int, int>, _U2 = int, _T1 = int, _T2 = int]':
test.cpp:25: instantiated from here
/home/vsekhar/gcc-4.1.1/lib/gcc/i686-pc-cygwin/4.1.1/../../../../include/c++/4.1.1/bits/stl_pair.h:90:
error: cannot convert 'const std::pair<int, int>' to 'int' in initialization

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

-v



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