integral_promotion.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Copyright 2005 Alexander Nasonov.
  2. // Distributed under the Boost Software License, Version 1.0. (See
  3. // accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef FILE_boost_type_traits_integral_promotion_hpp_INCLUDED
  6. #define FILE_boost_type_traits_integral_promotion_hpp_INCLUDED
  7. #include <boost/config.hpp>
  8. #include <boost/type_traits/integral_constant.hpp>
  9. #include <boost/type_traits/is_const.hpp>
  10. #include <boost/type_traits/is_enum.hpp>
  11. #include <boost/type_traits/is_volatile.hpp>
  12. #include <boost/type_traits/remove_cv.hpp>
  13. namespace boost {
  14. namespace type_traits { namespace detail {
  15. // 4.5/2
  16. template <class T> struct need_promotion : public boost::is_enum<T> {};
  17. // 4.5/1
  18. template<> struct need_promotion<char > : public true_type {};
  19. template<> struct need_promotion<signed char > : public true_type {};
  20. template<> struct need_promotion<unsigned char > : public true_type {};
  21. template<> struct need_promotion<signed short int > : public true_type {};
  22. template<> struct need_promotion<unsigned short int> : public true_type {};
  23. // Specializations for non-standard types.
  24. // Type is promoted if it's smaller then int.
  25. #define BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(T) \
  26. template<> struct need_promotion<T> \
  27. : public integral_constant<bool, (sizeof(T) < sizeof(int))> {};
  28. // Same set of integral types as in boost/type_traits/is_integral.hpp.
  29. // Please, keep in sync.
  30. #if (defined(BOOST_INTEL_CXX_VERSION) && defined(_MSC_VER) && (BOOST_INTEL_CXX_VERSION <= 600)) \
  31. || (defined(__BORLANDC__) && (__BORLANDC__ == 0x600) && (_MSC_VER < 1300))
  32. // TODO: common macro for this #if. Or better yet, PP SEQ of non-standard types.
  33. BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(__int8 )
  34. BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(unsigned __int8 )
  35. BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(__int16 )
  36. BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(unsigned __int16)
  37. BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(__int32 )
  38. BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(unsigned __int32)
  39. #ifdef __BORLANDC__
  40. BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(unsigned __int64)
  41. BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE( __int64)
  42. #endif
  43. #endif
  44. #if defined(BOOST_HAS_LONG_LONG)
  45. BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(boost::ulong_long_type)
  46. BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(boost::long_long_type )
  47. #elif defined(BOOST_HAS_MS_INT64)
  48. BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(unsigned __int64)
  49. BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE( __int64)
  50. #endif
  51. #undef BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE
  52. #ifndef BOOST_NO_INTRINSIC_WCHAR_T
  53. // 4.5/2
  54. template<> struct need_promotion<wchar_t> : public true_type {};
  55. #endif
  56. // 4.5/3 (integral bit-field) is not supported.
  57. // 4.5/4
  58. template<> struct need_promotion<bool> : public true_type {};
  59. // Get promoted type by index and cv qualifiers.
  60. template<int Index, int IsConst, int IsVolatile> struct promote_from_index;
  61. #define BOOST_TT_AUX_PROMOTE_FROM_INDEX(N,T) \
  62. template<> struct promote_from_index<N,0,0> { typedef T type; }; \
  63. template<> struct promote_from_index<N,0,1> { typedef T volatile type; }; \
  64. template<> struct promote_from_index<N,1,0> { typedef T const type; }; \
  65. template<> struct promote_from_index<N,1,1> { typedef T const volatile type; };
  66. BOOST_TT_AUX_PROMOTE_FROM_INDEX(1, int )
  67. BOOST_TT_AUX_PROMOTE_FROM_INDEX(2, unsigned int )
  68. BOOST_TT_AUX_PROMOTE_FROM_INDEX(3, long )
  69. BOOST_TT_AUX_PROMOTE_FROM_INDEX(4, unsigned long)
  70. // WARNING: integral promotions to non-standard types
  71. // long long and __int64 are not defined by the standard.
  72. // Additional specialisations and overloads shouldn't
  73. // introduce ambiguity, though.
  74. #if defined(BOOST_HAS_LONG_LONG)
  75. BOOST_TT_AUX_PROMOTE_FROM_INDEX(5, boost::long_long_type )
  76. BOOST_TT_AUX_PROMOTE_FROM_INDEX(6, boost::ulong_long_type)
  77. #elif defined(BOOST_HAS_MS_INT64)
  78. BOOST_TT_AUX_PROMOTE_FROM_INDEX(7, __int64 )
  79. BOOST_TT_AUX_PROMOTE_FROM_INDEX(8, unsigned __int64)
  80. #endif
  81. #undef BOOST_TT_AUX_PROMOTE_FROM_INDEX
  82. // Define BOOST_TT_AUX_PROMOTED_INDEX_TESTER:
  83. #if !defined(BOOST_MSVC)
  84. template<int N>
  85. struct sized_type_for_promotion
  86. {
  87. typedef char (&type)[N];
  88. };
  89. #define BOOST_TT_AUX_PROMOTED_INDEX_TESTER(I,T) \
  90. sized_type_for_promotion<I>::type promoted_index_tester(T);
  91. #else
  92. #define BOOST_TT_AUX_PROMOTED_INDEX_TESTER(I,T) \
  93. char (&promoted_index_tester(T))[I];
  94. #endif
  95. BOOST_TT_AUX_PROMOTED_INDEX_TESTER(1, int )
  96. BOOST_TT_AUX_PROMOTED_INDEX_TESTER(2, unsigned int )
  97. BOOST_TT_AUX_PROMOTED_INDEX_TESTER(3, long )
  98. BOOST_TT_AUX_PROMOTED_INDEX_TESTER(4, unsigned long)
  99. #if defined(BOOST_HAS_LONG_LONG)
  100. BOOST_TT_AUX_PROMOTED_INDEX_TESTER(5, boost::long_long_type )
  101. BOOST_TT_AUX_PROMOTED_INDEX_TESTER(6, boost::ulong_long_type)
  102. #elif defined(BOOST_HAS_MS_INT64)
  103. BOOST_TT_AUX_PROMOTED_INDEX_TESTER(7, __int64 )
  104. BOOST_TT_AUX_PROMOTED_INDEX_TESTER(8, unsigned __int64)
  105. #endif
  106. #undef BOOST_TT_AUX_PROMOTED_INDEX_TESTER
  107. // Get an index of promoted type for type T.
  108. // Precondition: need_promotion<T>
  109. template<class T>
  110. struct promoted_index
  111. {
  112. static T testee; // undefined
  113. BOOST_STATIC_CONSTANT(int, value = sizeof(promoted_index_tester(+testee)) );
  114. // Unary plus promotes testee LOOK HERE ---> ^
  115. };
  116. template<class T>
  117. struct integral_promotion_impl
  118. {
  119. typedef BOOST_DEDUCED_TYPENAME promote_from_index<
  120. (boost::type_traits::detail::promoted_index<T>::value)
  121. , (boost::is_const<T>::value)
  122. , (boost::is_volatile<T>::value)
  123. >::type type;
  124. };
  125. template<class T, bool b> struct integral_promotion { typedef T type; };
  126. template<class T> struct integral_promotion<T, true> : public integral_promotion_impl<T>{};
  127. } }
  128. template <class T> struct integral_promotion
  129. {
  130. private:
  131. typedef boost::type_traits::detail::need_promotion<typename remove_cv<T>::type> tag_type;
  132. public:
  133. typedef typename boost::type_traits::detail::integral_promotion<T, tag_type::value>::type type;
  134. };
  135. #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
  136. template <class T> using integral_promotion_t = typename integral_promotion<T>::type;
  137. #endif
  138. }
  139. #endif // #ifndef FILE_boost_type_traits_integral_promotion_hpp_INCLUDED