is_base_and_derived.hpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // (C) Copyright Rani Sharoni 2003.
  2. // Use, modification and distribution are subject to the Boost Software License,
  3. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt).
  5. //
  6. // See http://www.boost.org/libs/type_traits for most recent version including documentation.
  7. #ifndef BOOST_TT_IS_BASE_AND_DERIVED_HPP_INCLUDED
  8. #define BOOST_TT_IS_BASE_AND_DERIVED_HPP_INCLUDED
  9. #include <boost/type_traits/intrinsics.hpp>
  10. #include <boost/type_traits/integral_constant.hpp>
  11. #ifndef BOOST_IS_BASE_OF
  12. #include <boost/type_traits/is_class.hpp>
  13. #include <boost/type_traits/is_same.hpp>
  14. #include <boost/type_traits/is_convertible.hpp>
  15. #include <boost/config.hpp>
  16. #include <boost/static_assert.hpp>
  17. #endif
  18. #include <boost/type_traits/remove_cv.hpp>
  19. #include <boost/type_traits/is_same.hpp>
  20. namespace boost {
  21. namespace detail {
  22. #ifndef BOOST_IS_BASE_OF
  23. #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x581)) \
  24. && !BOOST_WORKAROUND(__SUNPRO_CC , <= 0x540) \
  25. && !BOOST_WORKAROUND(__EDG_VERSION__, <= 243) \
  26. && !BOOST_WORKAROUND(__DMC__, BOOST_TESTED_AT(0x840))
  27. // The EDG version number is a lower estimate.
  28. // It is not currently known which EDG version
  29. // exactly fixes the problem.
  30. /*************************************************************************
  31. This version detects ambiguous base classes and private base classes
  32. correctly, and was devised by Rani Sharoni.
  33. Explanation by Terje Slettebo and Rani Sharoni.
  34. Let's take the multiple base class below as an example, and the following
  35. will also show why there's not a problem with private or ambiguous base
  36. class:
  37. struct B {};
  38. struct B1 : B {};
  39. struct B2 : B {};
  40. struct D : private B1, private B2 {};
  41. is_base_and_derived<B, D>::value;
  42. First, some terminology:
  43. SC - Standard conversion
  44. UDC - User-defined conversion
  45. A user-defined conversion sequence consists of an SC, followed by an UDC,
  46. followed by another SC. Either SC may be the identity conversion.
  47. When passing the default-constructed Host object to the overloaded check_sig()
  48. functions (initialization 8.5/14/4/3), we have several viable implicit
  49. conversion sequences:
  50. For "static no_type check_sig(B const volatile *, int)" we have the conversion
  51. sequences:
  52. C -> C const (SC - Qualification Adjustment) -> B const volatile* (UDC)
  53. C -> D const volatile* (UDC) -> B1 const volatile* / B2 const volatile* ->
  54. B const volatile* (SC - Conversion)
  55. For "static yes_type check_sig(D const volatile *, T)" we have the conversion
  56. sequence:
  57. C -> D const volatile* (UDC)
  58. According to 13.3.3.1/4, in context of user-defined conversion only the
  59. standard conversion sequence is considered when selecting the best viable
  60. function, so it only considers up to the user-defined conversion. For the
  61. first function this means choosing between C -> C const and C -> C, and it
  62. chooses the latter, because it's a proper subset (13.3.3.2/3/2) of the
  63. former. Therefore, we have:
  64. C -> D const volatile* (UDC) -> B1 const volatile* / B2 const volatile* ->
  65. B const volatile* (SC - Conversion)
  66. C -> D const volatile* (UDC)
  67. Here, the principle of the "shortest subsequence" applies again, and it
  68. chooses C -> D const volatile*. This shows that it doesn't even need to
  69. consider the multiple paths to B, or accessibility, as that possibility is
  70. eliminated before it could possibly cause ambiguity or access violation.
  71. If D is not derived from B, it has to choose between C -> C const -> B const
  72. volatile* for the first function, and C -> D const volatile* for the second
  73. function, which are just as good (both requires a UDC, 13.3.3.2), had it not
  74. been for the fact that "static no_type check_sig(B const volatile *, int)" is
  75. not templated, which makes C -> C const -> B const volatile* the best choice
  76. (13.3.3/1/4), resulting in "no".
  77. Also, if Host::operator B const volatile* hadn't been const, the two
  78. conversion sequences for "static no_type check_sig(B const volatile *, int)", in
  79. the case where D is derived from B, would have been ambiguous.
  80. See also
  81. http://groups.google.com/groups?selm=df893da6.0301280859.522081f7%40posting.
  82. google.com and links therein.
  83. *************************************************************************/
  84. template <typename B, typename D>
  85. struct bd_helper
  86. {
  87. //
  88. // This VC7.1 specific workaround stops the compiler from generating
  89. // an internal compiler error when compiling with /vmg (thanks to
  90. // Aleksey Gurtovoy for figuring out the workaround).
  91. //
  92. #if !BOOST_WORKAROUND(BOOST_MSVC, == 1310)
  93. template <typename T>
  94. static type_traits::yes_type check_sig(D const volatile *, T);
  95. static type_traits::no_type check_sig(B const volatile *, int);
  96. #else
  97. static type_traits::yes_type check_sig(D const volatile *, long);
  98. static type_traits::no_type check_sig(B const volatile * const&, int);
  99. #endif
  100. };
  101. template<typename B, typename D>
  102. struct is_base_and_derived_impl2
  103. {
  104. #if BOOST_WORKAROUND(BOOST_MSVC_FULL_VER, >= 140050000)
  105. #pragma warning(push)
  106. #pragma warning(disable:6334)
  107. #endif
  108. //
  109. // May silently do the wrong thing with incomplete types
  110. // unless we trap them here:
  111. //
  112. BOOST_STATIC_ASSERT(sizeof(B) != 0);
  113. BOOST_STATIC_ASSERT(sizeof(D) != 0);
  114. struct Host
  115. {
  116. #if !BOOST_WORKAROUND(BOOST_MSVC, == 1310)
  117. operator B const volatile *() const;
  118. #else
  119. operator B const volatile * const&() const;
  120. #endif
  121. operator D const volatile *();
  122. };
  123. BOOST_STATIC_CONSTANT(bool, value =
  124. sizeof(bd_helper<B,D>::check_sig(Host(), 0)) == sizeof(type_traits::yes_type));
  125. #if BOOST_WORKAROUND(BOOST_MSVC_FULL_VER, >= 140050000)
  126. #pragma warning(pop)
  127. #endif
  128. };
  129. #else
  130. //
  131. // broken version:
  132. //
  133. template<typename B, typename D>
  134. struct is_base_and_derived_impl2
  135. {
  136. BOOST_STATIC_CONSTANT(bool, value =
  137. (::boost::is_convertible<D*,B*>::value));
  138. };
  139. #define BOOST_BROKEN_IS_BASE_AND_DERIVED
  140. #endif
  141. template <typename B, typename D>
  142. struct is_base_and_derived_impl3
  143. {
  144. BOOST_STATIC_CONSTANT(bool, value = false);
  145. };
  146. template <bool ic1, bool ic2, bool iss>
  147. struct is_base_and_derived_select
  148. {
  149. template <class T, class U>
  150. struct rebind
  151. {
  152. typedef is_base_and_derived_impl3<T,U> type;
  153. };
  154. };
  155. template <>
  156. struct is_base_and_derived_select<true,true,false>
  157. {
  158. template <class T, class U>
  159. struct rebind
  160. {
  161. typedef is_base_and_derived_impl2<T,U> type;
  162. };
  163. };
  164. template <typename B, typename D>
  165. struct is_base_and_derived_impl
  166. {
  167. typedef typename remove_cv<B>::type ncvB;
  168. typedef typename remove_cv<D>::type ncvD;
  169. typedef is_base_and_derived_select<
  170. ::boost::is_class<B>::value,
  171. ::boost::is_class<D>::value,
  172. ::boost::is_same<ncvB,ncvD>::value> selector;
  173. typedef typename selector::template rebind<ncvB,ncvD> binder;
  174. typedef typename binder::type bound_type;
  175. BOOST_STATIC_CONSTANT(bool, value = bound_type::value);
  176. };
  177. #else
  178. template <typename B, typename D>
  179. struct is_base_and_derived_impl
  180. {
  181. typedef typename remove_cv<B>::type ncvB;
  182. typedef typename remove_cv<D>::type ncvD;
  183. BOOST_STATIC_CONSTANT(bool, value = (BOOST_IS_BASE_OF(B,D) && ! ::boost::is_same<ncvB,ncvD>::value));
  184. };
  185. #endif
  186. } // namespace detail
  187. template <class Base, class Derived> struct is_base_and_derived
  188. : public integral_constant<bool, (::boost::detail::is_base_and_derived_impl<Base, Derived>::value)> {};
  189. template <class Base, class Derived> struct is_base_and_derived<Base&, Derived> : public false_type{};
  190. template <class Base, class Derived> struct is_base_and_derived<Base, Derived&> : public false_type{};
  191. template <class Base, class Derived> struct is_base_and_derived<Base&, Derived&> : public false_type{};
  192. #if BOOST_WORKAROUND(__CODEGEARC__, BOOST_TESTED_AT(0x610))
  193. template <class Base> struct is_base_and_derived<Base, Base> : public true_type{};
  194. #endif
  195. } // namespace boost
  196. #endif // BOOST_TT_IS_BASE_AND_DERIVED_HPP_INCLUDED