1 /* boost random/traits.hpp header file
3 * Copyright John Maddock 2015
4 * Distributed under the Boost Software License, Version 1.0. (See
5 * accompanying file LICENSE_1_0.txt or copy at
6 * http://www.boost.org/LICENSE_1_0.txt)
8 * See http://www.boost.org for most recent version including documentation.
10 * These traits classes serve two purposes: they are designed to mostly
11 * work out of the box for multiprecision types (ie number types that are
12 * C++ class types and not integers or floats from type-traits point of view),
13 * they are also a potential point of specialization for user-defined
19 #ifndef BOOST_RANDOM_TRAITS_HPP
20 #define BOOST_RANDOM_TRAITS_HPP
22 #include <boost/type_traits/is_signed.hpp>
23 #include <boost/type_traits/is_integral.hpp>
24 #include <boost/type_traits/make_unsigned.hpp>
25 #include <boost/mpl/bool.hpp>
32 template <class T, bool intrinsic>
33 struct make_unsigned_imp
35 typedef typename boost::make_unsigned<T>::type type;
38 struct make_unsigned_imp<T, false>
40 BOOST_STATIC_ASSERT(std::numeric_limits<T>::is_specialized);
41 BOOST_STATIC_ASSERT(std::numeric_limits<T>::is_signed == false);
42 BOOST_STATIC_ASSERT(std::numeric_limits<T>::is_integer == true);
46 /** \brief Converts the argument type T to an unsigned type.
48 * This trait has a single member `type` which is the unsigned type corresponding to T.
50 * if T is signed, then member `type` *should define a type with one more bit precision than T*. For built-in
51 * types this trait defaults to `boost::make_unsigned<T>::type`. For user defined types it simply asserts that
52 * the argument type T is an unsigned integer (using std::numeric_limits).
53 * User defined specializations may be provided for other cases.
58 : public make_unsigned_imp < T, boost::is_integral<T>::value >
62 template <class T, bool intrinsic>
63 struct make_unsigned_or_unbounded_imp
65 typedef typename boost::make_unsigned<T>::type type;
68 struct make_unsigned_or_unbounded_imp<T, false>
70 BOOST_STATIC_ASSERT(std::numeric_limits<T>::is_specialized);
71 BOOST_STATIC_ASSERT((std::numeric_limits<T>::is_signed == false) || (std::numeric_limits<T>::is_bounded == false));
72 BOOST_STATIC_ASSERT(std::numeric_limits<T>::is_integer == true);
76 /** \brief Converts the argument type T to either an unsigned type or an unbounded integer type.
78 * This trait has a single member `type` which is either the unsigned type corresponding to T or an unbounded
79 * integer type. This trait is used to generate types suitable for the calculation of a range: as a result
80 * if T is signed, then member `type` *should define a type with one more bit precision than T*. For built-in
81 * types this trait defaults to `boost::make_unsigned<T>::type`. For user defined types it simply asserts that
82 * the argument type T is either an unbounded integer, or an unsigned one (using std::numeric_limits).
83 * User defined specializations may be provided for other cases.
86 struct make_unsigned_or_unbounded
88 : public make_unsigned_or_unbounded_imp < T, boost::is_integral<T>::value >
91 /** \brief Traits class that indicates whether type T is an integer
95 : public mpl::bool_<boost::is_integral<T>::value || (std::numeric_limits<T>::is_integer)>
97 /** \brief Traits class that indicates whether type T is a signed integer
99 template <class T> struct is_signed
100 : public mpl::bool_ < boost::is_signed<T>::value || (std::numeric_limits<T>::is_specialized && std::numeric_limits<T>::is_integer && std::numeric_limits<T>::is_signed)>