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)
6 #ifndef FILE_boost_type_traits_integral_promotion_hpp_INCLUDED
7 #define FILE_boost_type_traits_integral_promotion_hpp_INCLUDED
9 #include <boost/config.hpp>
11 #include <boost/mpl/eval_if.hpp>
12 #include <boost/mpl/identity.hpp>
13 #include <boost/type_traits/integral_constant.hpp>
14 #include <boost/type_traits/is_const.hpp>
15 #include <boost/type_traits/is_enum.hpp>
16 #include <boost/type_traits/is_volatile.hpp>
17 #include <boost/type_traits/remove_cv.hpp>
19 // Should be the last #include
20 #include <boost/type_traits/detail/type_trait_def.hpp>
24 namespace type_traits { namespace detail {
27 template <class T> struct need_promotion : boost::is_enum<T> {};
30 template<> struct need_promotion<char > : true_type {};
31 template<> struct need_promotion<signed char > : true_type {};
32 template<> struct need_promotion<unsigned char > : true_type {};
33 template<> struct need_promotion<signed short int > : true_type {};
34 template<> struct need_promotion<unsigned short int> : true_type {};
37 // Specializations for non-standard types.
38 // Type is promoted if it's smaller then int.
40 #define BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(T) \
41 template<> struct need_promotion<T> \
42 : integral_constant<bool, (sizeof(T) < sizeof(int))> {};
44 // Same set of integral types as in boost/type_traits/is_integral.hpp.
45 // Please, keep in sync.
46 #if (defined(BOOST_MSVC) && (BOOST_MSVC < 1300)) \
47 || (defined(BOOST_INTEL_CXX_VERSION) && defined(_MSC_VER) && (BOOST_INTEL_CXX_VERSION <= 600)) \
48 || (defined(__BORLANDC__) && (__BORLANDC__ == 0x600) && (_MSC_VER < 1300))
49 // TODO: common macro for this #if. Or better yet, PP SEQ of non-standard types.
50 BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(__int8 )
51 BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(unsigned __int8 )
52 BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(__int16 )
53 BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(unsigned __int16)
54 BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(__int32 )
55 BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(unsigned __int32)
57 BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(unsigned __int64)
58 BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE( __int64)
62 #if defined(BOOST_HAS_LONG_LONG)
63 BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(boost::ulong_long_type)
64 BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(boost::long_long_type )
65 #elif defined(BOOST_HAS_MS_INT64)
66 BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE(unsigned __int64)
67 BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE( __int64)
70 #undef BOOST_TT_AUX_PROMOTE_NONSTANDARD_TYPE
73 #ifndef BOOST_NO_INTRINSIC_WCHAR_T
75 template<> struct need_promotion<wchar_t> : true_type {};
78 // 4.5/3 (integral bit-field) is not supported.
81 template<> struct need_promotion<bool> : true_type {};
84 // Get promoted type by index and cv qualifiers.
86 template<int Index, int IsConst, int IsVolatile> struct promote_from_index;
88 #define BOOST_TT_AUX_PROMOTE_FROM_INDEX(N,T) \
89 template<> struct promote_from_index<N,0,0> { typedef T type; }; \
90 template<> struct promote_from_index<N,0,1> { typedef T volatile type; }; \
91 template<> struct promote_from_index<N,1,0> { typedef T const type; }; \
92 template<> struct promote_from_index<N,1,1> { typedef T const volatile type; };
95 BOOST_TT_AUX_PROMOTE_FROM_INDEX(1, int )
96 BOOST_TT_AUX_PROMOTE_FROM_INDEX(2, unsigned int )
97 BOOST_TT_AUX_PROMOTE_FROM_INDEX(3, long )
98 BOOST_TT_AUX_PROMOTE_FROM_INDEX(4, unsigned long)
101 // WARNING: integral promotions to non-standard types
102 // long long and __int64 are not defined by the standard.
103 // Additional specialisations and overloads shouldn't
104 // introduce ambiguity, though.
106 #if defined(BOOST_HAS_LONG_LONG)
107 BOOST_TT_AUX_PROMOTE_FROM_INDEX(5, boost::long_long_type )
108 BOOST_TT_AUX_PROMOTE_FROM_INDEX(6, boost::ulong_long_type)
109 #elif defined(BOOST_HAS_MS_INT64)
110 BOOST_TT_AUX_PROMOTE_FROM_INDEX(7, __int64 )
111 BOOST_TT_AUX_PROMOTE_FROM_INDEX(8, unsigned __int64)
114 #undef BOOST_TT_AUX_PROMOTE_FROM_INDEX
117 // Define BOOST_TT_AUX_PROMOTED_INDEX_TESTER:
118 #if !defined(BOOST_MSVC)
121 struct sized_type_for_promotion
123 typedef char (&type)[N];
126 #define BOOST_TT_AUX_PROMOTED_INDEX_TESTER(I,T) \
127 sized_type_for_promotion<I>::type promoted_index_tester(T);
131 #define BOOST_TT_AUX_PROMOTED_INDEX_TESTER(I,T) \
132 char (&promoted_index_tester(T))[I];
136 BOOST_TT_AUX_PROMOTED_INDEX_TESTER(1, int )
137 BOOST_TT_AUX_PROMOTED_INDEX_TESTER(2, unsigned int )
138 BOOST_TT_AUX_PROMOTED_INDEX_TESTER(3, long )
139 BOOST_TT_AUX_PROMOTED_INDEX_TESTER(4, unsigned long)
141 #if defined(BOOST_HAS_LONG_LONG)
142 BOOST_TT_AUX_PROMOTED_INDEX_TESTER(5, boost::long_long_type )
143 BOOST_TT_AUX_PROMOTED_INDEX_TESTER(6, boost::ulong_long_type)
144 #elif defined(BOOST_HAS_MS_INT64)
145 BOOST_TT_AUX_PROMOTED_INDEX_TESTER(7, __int64 )
146 BOOST_TT_AUX_PROMOTED_INDEX_TESTER(8, unsigned __int64)
149 #undef BOOST_TT_AUX_PROMOTED_INDEX_TESTER
152 // Get an index of promoted type for type T.
153 // Precondition: need_promotion<T>
155 struct promoted_index
157 static T testee; // undefined
158 BOOST_STATIC_CONSTANT(int, value = sizeof(promoted_index_tester(+testee)) );
159 // Unary plus promotes testee LOOK HERE ---> ^
163 struct integral_promotion_impl
165 typedef BOOST_DEDUCED_TYPENAME promote_from_index<
166 (boost::type_traits::detail::promoted_index<T>::value)
167 , (boost::is_const<T>::value)
168 , (boost::is_volatile<T>::value)
173 struct integral_promotion
174 : boost::mpl::eval_if<
175 need_promotion<BOOST_DEDUCED_TYPENAME remove_cv<T>::type>
176 , integral_promotion_impl<T>
177 , boost::mpl::identity<T>
184 BOOST_TT_AUX_TYPE_TRAIT_DEF1(
187 , BOOST_DEDUCED_TYPENAME
188 boost::type_traits::detail::integral_promotion<T>::type
192 #include <boost/type_traits/detail/type_trait_undef.hpp>
194 #endif // #ifndef FILE_boost_type_traits_integral_promotion_hpp_INCLUDED