Boost logo

Boost Users :

From: Charles Wilson (boost_at_[hidden])
Date: 2005-07-06 00:50:53


When compiling the is_convertible test case, I'm getting the following
compile warnings:

------------------------------------
/usr/local/src/boost/boost_1_32_0/boost/type_traits/is_convertible.hpp:
In instantiation of `boost::detail::is_convertible_basic_impl<float&,
convertible_from<char> >':
/usr/local/src/boost/boost_1_32_0/boost/type_traits/is_convertible.hpp:224:
   instantiated from `boost::detail::is_convertible_impl<float,
convertible_from<char> >'
/usr/local/src/boost/boost_1_32_0/boost/type_traits/is_convertible.hpp:279:
   instantiated from `boost::is_convertible<float, convertible_from<char> >'
/usr/local/src/boost/boost_1_32_0/libs/type_traits/test/is_convertible_test.cpp:24:
   instantiated from here
/usr/local/src/boost/boost_1_32_0/boost/type_traits/is_convertible.hpp:124:
warning: passing `float' for converting 1 of
`convertible_from<T>::convertible_from(T) [with T = char]'
/usr/local/src/boost/boost_1_32_0/boost/type_traits/is_convertible.hpp:
In instantiation of `boost::detail::is_convertible_basic_impl<float&,
convertible_from<const char&> >':
/usr/local/src/boost/boost_1_32_0/boost/type_traits/is_convertible.hpp:224:
   instantiated from `boost::detail::is_convertible_impl<float,
convertible_from<const char&> >'
/usr/local/src/boost/boost_1_32_0/boost/type_traits/is_convertible.hpp:279:
   instantiated from `boost::is_convertible<float,
convertible_from<const char&> >'
/usr/local/src/boost/boost_1_32_0/libs/type_traits/test/is_convertible_test.cpp:25:
   instantiated from here
/usr/local/src/boost/boost_1_32_0/boost/type_traits/is_convertible.hpp:124:
warning: passing `float' for converting 1 of
`convertible_from<T>::convertible_from(T) [with T = const char&]'
------------------------------------

The test cases in question are

BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_convertible<float,convertible_from<char>
>::value), true);
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_convertible<float,convertible_from<char
const&> >::value), true);

I think this is caused by trying to call the convertible_from<char>
constructor (which takes a char) with a float& argument, which forces an
implicit conversion from float-to-char BEFORE the "user defined
conversion" of char-to-convertible_from<char>.

gcc issues this warning when doing a float(or double) conversion to an
integral type during template instantiation/overload resolution -- but
does not warn if "merely" truncating from a wider integral
representation down to char. I can see why gcc might warn in each case
-- but
   (1) warning in one case but not the other seems counterintuitive
   (2) in any case, I'd like to suppress or avoid both sorts of warnings

Does anybody have an idea how to avoid these warnings with gcc -- or can
verify that they occur with other versions of gcc (like 4.0?).

It's also possible that I'm confused :-) and gcc's behavior is correct;
but that means that boost::is_convertible is deliberately violating some
rule or other and causing these warnings -- and I doubt that is the case.

I've pasted the relevant parts of the is_convertible code below; the
lines which I *think* are the offending ones are marked with '@@'.

     template <typename T> struct checker
     {
         static boost::type_traits::no_type _m_check(any_conversion ...);
@@ static boost::type_traits::yes_type _m_check(T, int);
     };

     template <typename From, typename To>
     struct is_convertible_basic_impl
     {
         static From _m_from;
         static bool const value = sizeof(
@@ detail::checker<To>::_m_check(_m_from, 0) )
             == sizeof(::boost::type_traits::yes_type);
     };

     template <typename From, typename To>
     struct is_convertible_impl
     {
       typedef typename add_reference<From>::type ref_type;
       static const bool value = (::boost::type_traits::ice_and<
          ::boost::detail::is_convertible_basic_impl<ref_type,To>::value,
          ::boost::type_traits::ice_not< ::boost::is_array<To>::value
>::value >::value);
     };

     template< typename From, typename To > struct is_convertible
        : mpl_::bool_<
            (::boost::detail::is_convertible_impl<From,To>::value)
>
     { };

Thanks for any help, tips, or suggestions.

--
Chuck

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