make_signed.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // (C) Copyright John Maddock 2007.
  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_MAKE_SIGNED_HPP_INCLUDED
  8. #define BOOST_TT_MAKE_SIGNED_HPP_INCLUDED
  9. #include <boost/type_traits/conditional.hpp>
  10. #include <boost/type_traits/is_integral.hpp>
  11. #include <boost/type_traits/is_signed.hpp>
  12. #include <boost/type_traits/is_unsigned.hpp>
  13. #include <boost/type_traits/is_enum.hpp>
  14. #include <boost/type_traits/is_same.hpp>
  15. #include <boost/type_traits/remove_cv.hpp>
  16. #include <boost/type_traits/is_const.hpp>
  17. #include <boost/type_traits/is_volatile.hpp>
  18. #include <boost/type_traits/add_const.hpp>
  19. #include <boost/type_traits/add_volatile.hpp>
  20. #include <boost/static_assert.hpp>
  21. namespace boost {
  22. template <class T>
  23. struct make_signed
  24. {
  25. private:
  26. BOOST_STATIC_ASSERT_MSG(( ::boost::is_integral<T>::value || ::boost::is_enum<T>::value), "The template argument to make_signed must be an integer or enum type.");
  27. BOOST_STATIC_ASSERT_MSG(!(::boost::is_same<typename remove_cv<T>::type, bool>::value), "The template argument to make_signed must not be the type bool.");
  28. typedef typename remove_cv<T>::type t_no_cv;
  29. typedef typename conditional<
  30. (::boost::is_signed<T>::value
  31. && ::boost::is_integral<T>::value
  32. && ! ::boost::is_same<t_no_cv, char>::value
  33. && ! ::boost::is_same<t_no_cv, wchar_t>::value
  34. && ! ::boost::is_same<t_no_cv, bool>::value),
  35. T,
  36. typename conditional<
  37. (::boost::is_integral<T>::value
  38. && ! ::boost::is_same<t_no_cv, char>::value
  39. && ! ::boost::is_same<t_no_cv, wchar_t>::value
  40. && ! ::boost::is_same<t_no_cv, bool>::value),
  41. typename conditional<
  42. is_same<t_no_cv, unsigned char>::value,
  43. signed char,
  44. typename conditional<
  45. is_same<t_no_cv, unsigned short>::value,
  46. signed short,
  47. typename conditional<
  48. is_same<t_no_cv, unsigned int>::value,
  49. int,
  50. typename conditional<
  51. is_same<t_no_cv, unsigned long>::value,
  52. long,
  53. #if defined(BOOST_HAS_LONG_LONG)
  54. #ifdef BOOST_HAS_INT128
  55. typename conditional<
  56. sizeof(t_no_cv) == sizeof(boost::long_long_type),
  57. boost::long_long_type,
  58. boost::int128_type
  59. >::type
  60. #else
  61. boost::long_long_type
  62. #endif
  63. #elif defined(BOOST_HAS_MS_INT64)
  64. __int64
  65. #else
  66. long
  67. #endif
  68. >::type
  69. >::type
  70. >::type
  71. >::type,
  72. // Not a regular integer type:
  73. typename conditional<
  74. sizeof(t_no_cv) == sizeof(unsigned char),
  75. signed char,
  76. typename conditional<
  77. sizeof(t_no_cv) == sizeof(unsigned short),
  78. signed short,
  79. typename conditional<
  80. sizeof(t_no_cv) == sizeof(unsigned int),
  81. int,
  82. typename conditional<
  83. sizeof(t_no_cv) == sizeof(unsigned long),
  84. long,
  85. #if defined(BOOST_HAS_LONG_LONG)
  86. #ifdef BOOST_HAS_INT128
  87. typename conditional<
  88. sizeof(t_no_cv) == sizeof(boost::long_long_type),
  89. boost::long_long_type,
  90. boost::int128_type
  91. >::type
  92. #else
  93. boost::long_long_type
  94. #endif
  95. #elif defined(BOOST_HAS_MS_INT64)
  96. __int64
  97. #else
  98. long
  99. #endif
  100. >::type
  101. >::type
  102. >::type
  103. >::type
  104. >::type
  105. >::type base_integer_type;
  106. // Add back any const qualifier:
  107. typedef typename conditional<
  108. is_const<T>::value,
  109. typename add_const<base_integer_type>::type,
  110. base_integer_type
  111. >::type const_base_integer_type;
  112. public:
  113. // Add back any volatile qualifier:
  114. typedef typename conditional<
  115. is_volatile<T>::value,
  116. typename add_volatile<const_base_integer_type>::type,
  117. const_base_integer_type
  118. >::type type;
  119. };
  120. #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
  121. template <class T> using make_signed_t = typename make_signed<T>::type;
  122. #endif
  123. } // namespace boost
  124. #endif // BOOST_TT_ADD_REFERENCE_HPP_INCLUDED