Widened the range of types that may be passed to is_convertible.

Extended is_base_and_derived to cope with ambiguous base classes
and non-public base classes.


[SVN r17146]
This commit is contained in:
John Maddock
2003-02-01 12:11:10 +00:00
parent 9ad8445156
commit 524468f579
2 changed files with 108 additions and 11 deletions

View File

@ -1,5 +1,5 @@
// (C) Copyright Steve Cleary, Beman Dawes, Howard Hinnant & John Maddock 2000.
// (C) Copyright Rani Sharoni 2002.
// Permission to copy, use, modify, sell and distribute this software is
// granted provided this copyright notice appears in all copies. This software
// is provided "as is" without express or implied warranty, and with no claim
@ -11,8 +11,10 @@
#define BOOST_TT_IS_BASE_AND_DERIVED_HPP_INCLUDED
#include "boost/type_traits/is_class.hpp"
#include "boost/type_traits/is_same.hpp"
#include "boost/type_traits/is_convertible.hpp"
#include "boost/type_traits/detail/ice_and.hpp"
#include "boost/type_traits/remove_cv.hpp"
#include "boost/config.hpp"
// should be the last #include
@ -22,16 +24,92 @@ namespace boost {
namespace detail {
template <typename Base, typename Derived>
#if !defined(__BORLANDC__)
/*************************************************************************
This version detects ambiguous base classes and private base classes
correctly, and was devised by Rani Sharoni.
*************************************************************************/
template <typename B, typename D>
struct bd_helper
{
template <typename T>
static type_traits::yes_type check(D const volatile *, T);
static type_traits::no_type check(B const volatile *, int);
};
template<typename B, typename D>
struct is_base_and_derived_impl2
{
struct Host
{
operator B const volatile *() const;
operator D const volatile *();
};
BOOST_STATIC_CONSTANT(bool, value =
sizeof(bd_helper<B,D>::check(Host(), 0)) == sizeof(type_traits::yes_type));
};
#else
//
// broken version:
//
template<typename B, typename D>
struct is_base_and_derived_impl2
{
BOOST_STATIC_CONSTANT(bool, value =
(::boost::is_convertible<D*,B*>::value));
};
#define BOOST_BROKEN_IS_BASE_AND_DERIVED
#endif
template <typename B, typename D>
struct is_base_and_derived_impl3
{
BOOST_STATIC_CONSTANT(bool, value = false);
};
template <bool ic1, bool ic2, bool iss>
struct is_base_and_derived_select
{
template <class T, class U>
struct rebind
{
typedef is_base_and_derived_impl3<T,U> type;
};
};
template <>
struct is_base_and_derived_select<true,true,false>
{
template <class T, class U>
struct rebind
{
typedef is_base_and_derived_impl2<T,U> type;
};
};
template <typename B, typename D>
struct is_base_and_derived_impl
{
BOOST_STATIC_CONSTANT(bool, value =
(::boost::type_traits::ice_and<
::boost::is_convertible<Derived*,Base*>::value,
::boost::is_class<Derived>::value,
::boost::is_class<Base>::value
>::value)
);
typedef typename remove_cv<B>::type ncvB;
typedef typename remove_cv<D>::type ncvD;
typedef is_base_and_derived_select<
::boost::is_class<B>::value,
::boost::is_class<D>::value,
::boost::is_same<B,D>::value> selector;
typedef typename selector::template rebind<ncvB,ncvD> binder;
typedef typename binder::type bound_type;
BOOST_STATIC_CONSTANT(bool, value = bound_type::value);
};
} // namespace detail
@ -54,3 +132,5 @@ BOOST_TT_AUX_BOOL_TRAIT_PARTIAL_SPEC2_2(typename Base,typename Derived,is_base_a
#include "boost/type_traits/detail/bool_trait_undef.hpp"
#endif // BOOST_TT_IS_BASE_AND_DERIVED_HPP_INCLUDED

View File

@ -18,6 +18,9 @@
#include "boost/type_traits/detail/yes_no_type.hpp"
#include "boost/type_traits/config.hpp"
#include "boost/type_traits/is_array.hpp"
#include "boost/type_traits/add_reference.hpp"
#include "boost/type_traits/ice.hpp"
#if defined(BOOST_MSVC) && (BOOST_MSVC <= 1300)
# include "boost/type_traits/is_void.hpp"
@ -166,16 +169,30 @@ struct is_convertible_impl
static ::boost::type_traits::yes_type BOOST_TT_DECL _m_check(To);
static From _m_from;
BOOST_STATIC_CONSTANT(bool, value =
BOOST_STATIC_CONSTANT(bool, value =
sizeof( _m_check(_m_from) ) == sizeof(::boost::type_traits::yes_type)
);
};
#endif // is_convertible_impl
template <typename From, typename To>
struct is_convertible_forwarder
{
typedef typename add_reference<From>::type ref_type;
BOOST_STATIC_CONSTANT(bool, value =
(::boost::type_traits::ice_and<
::boost::detail::is_convertible_impl<ref_type,To>::value,
::boost::type_traits::ice_not<
::boost::is_array<To>::value
>::value
>::value)
);
};
} // namespace detail
BOOST_TT_AUX_BOOL_TRAIT_DEF2(is_convertible,From,To,(::boost::detail::is_convertible_impl<From,To>::value))
BOOST_TT_AUX_BOOL_TRAIT_DEF2(is_convertible,From,To,(::boost::detail::is_convertible_forwarder<From,To>::value))
//
// Now add the full and partial specialisations