1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
[/
Boost.Optional
Copyright (c) 2003-2007 Fernando Luis Cacciola Carballal
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at
http://www.boost.org/LICENSE_1_0.txt)
]
[#numeric_conversion_traits]
[section conversion_traits<> traits class]
[section Types]
[#numeric_conversion_traits_int_float_mixture_enum]
[section enumeration int_float_mixture_enum]
namespace boost { namespace numeric {
enum int_float_mixture_enum
{
integral_to_integral
,integral_to_float
,float_to_integral
,float_to_float
} ;
} } // namespace boost::numeric
[endsect]
[#numeric_conversion_traits_sign_mixture_enum]
[section enumeration sign_mixture_enum]
namespace boost { namespace numeric {
enum sign_mixture_enum
{
unsigned_to_unsigned
,signed_to_signed
,signed_to_unsigned
,unsigned_to_signed
} ;
} } // namespace boost::numeric
[endsect]
[#numeric_conversion_traits_udt_builtin_mixture_enum]
[section enumeration udt_builtin_mixture_enum]
namespace boost { namespace numeric {
enum udt_builtin_mixture_enum
{
builtin_to_builtin
,builtin_to_udt
,udt_to_builtin
,udt_to_udt
} ;
} } // namespace boost::numeric
[endsect]
[#numeric_conversion_traits_class_int_float_mixture]
[section template class int_float_mixture<>]
namespace boost { namespace numeric {
template <class T, class S>
struct int_float_mixture : mpl::integral_c<int_float_mixture_enum, impl-def-value> {} ;
} } // namespace boost::numeric
Classifying `S` and `T` as either integral or float, this __MPL_INTEGRAL_CONSTANT__
indicates the combination of these attributes.
Its `::value` is of enumeration type
[link numeric_conversion_traits_int_float_mixture_enum `boost::numeric::int_float_mixture_enum`]
[endsect]
[#numeric_conversion_traits_class_sign_mixture]
[section template class sign_mixture<>]
namespace boost { namespace numeric {
template <class T, class S>
struct sign_mixture : mpl::integral_c<sign_mixture_enum, impl-def-value> {} ;
} } // namespace boost::numeric
Classifying `S` and `T` as either signed or unsigned, this __MPL_INTEGRAL_CONSTANT__
indicates the combination of these attributes.
Its `::value` is of enumeration type
[link numeric_conversion_traits_sign_mixture_enum `boost::numeric::sign_mixture_enum`]
[endsect]
[#numeric_conversion_traits_class_udt_builtin_mixture]
[section template class udt_builtin_mixture<>]
namespace boost { namespace numeric {
template <class T, class S>
struct udt_builtin_mixture : mpl::integral_c<udt_builtin__mixture_enum, impl-def-value> {} ;
} } // namespace boost::numeric
Classifying `S` and `T` as either user-defined or builtin, this __MPL_INTEGRAL_CONSTANT__
indicates the combination of these attributes.
Its `::value` is of enumeration type
[link numeric_conversion_traits_udt_builtin_mixture_enum `boost::numeric::udt_builtin_mixture_enum`]
[endsect]
[#numeric_conversion_traits_is_subranged]
[section template class is_subranged<>]
namespace boost { namespace numeric {
template <class T, class S>
struct is_subranged : mpl::bool_<impl-def-value> {} ;
} } // namespace boost::numeric
Indicates if the range of the target type `T` is a subset of the range of the source
type `S`. That is: if there are some source values which fall out of the
Target type's range.
It is a boolean __MPL_INTEGRAL_CONSTANT__.
It does not indicate if a particular conversion is effectively out of range;
it indicates that some conversion might be out of range because not all the
source values are representable as Target type.
[endsect]
[section template class conversion_traits<>]
namespace boost { namespace numeric {
template <class T, class S>
struct conversion_traits
{
mpl::integral_c<int_float_mixture_enum , ...> int_float_mixture ;
mpl::integral_c<sign_mixture_enum , ...> sign_mixture;
mpl::integral_c<udt_builtin_mixture_enum, ...> udt_builtin_mixture ;
mpl::bool_<...> subranged ;
mpl::bool_<...> trivial ;
typedef T target_type ;
typedef S source_type ;
typedef ... argument_type ;
typedef ... result_type ;
typedef ... supertype ;
typedef ... subtype ;
} ;
} } // namespace numeric, namespace boost
This traits class indicates some properties of a ['numeric conversion] direction:
from a source type `S` to a target type `T`. It does not indicate the properties
of a ['specific] conversion, but of the conversion direction. See
[link numeric_conversion_definitions_subranged Definitions] for details.
The traits class provides the following __MPL_INTEGRAL_CONSTANT__\s of enumeration
type. They express the combination of certain attributes of the Source and
Target types (thus they are call mixture):
[table
[[ ][ ]]
[[[*int_float_mixture ]][
Same as given by the traits class
[link numeric_conversion_traits_class_int_float_mixture int_float_mixture]
]]
[[[*sign_mixture ]][
Same as given by the traits class
[link numeric_conversion_traits_class_sign_mixture sign_mixture]
]]
[[[*udt_builtin_mixture ]]
[Same as given by the traits class
[link numeric_conversion_traits_class_udt_builtin_mixture udt_builtin_mixture]
]]
]
The traits class provides the following __MPL_INTEGRAL_CONSTANT__\s of boolean type
which indicates indirectly the relation between the Source and Target ranges
(see [link numeric_conversion_definitions_range Definitions] for details).
[table
[[ ][ ]]
[[subranged ][
Same as given by [link numeric_conversion_traits_is_subranged is_subranged]
]]
[[trivial][
Indicates if both Source and Target, [_without cv-qualifications], are the same type.
Its `::value` is of boolean type.
]]
]
The traits class provides the following types. They are the Source and Target types classified
and qualified for different purposes.
[table
[[ ][ ]]
[[[*target_type]][
The template parameter `T` without cv-qualifications
]]
[[[*source_type]][
The template parameter `S` without cv-qualifications
]]
[[[*argument_type]][
This type is either source_type or `source_type const&`.
It represents the optimal argument type for the
[link numeric_conversion_converter converter] member functions.
If S is a built-in type, this is `source_type`, otherwise, this is `source_type const&`.
]]
[[[*result_type]][
This type is either target_type or target_type const&
It represents the return type of the
[link numeric_conversion_converter converter] member functions.
If `T==S`, it is `target_type const&`, otherwise, it is `target_type`.
]]
[[[*supertype]][
If the conversion is subranged, it is `source_type`, otherwise, it is `target_type`
]]
[[[*subtype]][
If the conversion is subranged, it is `target_type`, otherwise, it is `source_type`
]]
]
[endsect]
[endsect]
[section Examples]
#include <cassert>
#include <typeinfo>
#include <boost/numeric/conversion/conversion_traits.hpp>
int main()
{
// A trivial conversion.
typedef boost::numeric::conversion_traits<short,short> Short2Short_Traits ;
assert ( Short2Short_Traits::trivial::value ) ;
// A subranged conversion.
typedef boost::numeric::conversion_traits<double,unsigned int> UInt2Double_Traits ;
assert ( UInt2Double_Traits::int_float_mixture::value == boost::numeric::integral_to_float ) ;
assert ( UInt2Double_Traits::sign_mixture::value == boost::numeric::unsigned_to_signed ) ;
assert ( !UInt2Double_Traits::subranged::value ) ;
assert ( typeid(UInt2Double_Traits::supertype) == typeid(double) ) ;
assert ( typeid(UInt2Double_Traits::subtype) == typeid(unsigned int) ) ;
// A doubly subranged conversion.
assert ( (boost::numeric::conversion_traits<short, unsigned short>::subranged::value) );
assert ( (boost::numeric::conversion_traits<unsigned short, short>::subranged::value) );
return 0;
}
[endsect]
[endsect]
|