numeric_traits.hpp
Go to the documentation of this file.
00001 // (C) Copyright David Abrahams 2001, Howard Hinnant 2001.
00002 //
00003 // Distributed under the Boost Software License, Version 1.0. (See
00004 // accompanying file LICENSE_1_0.txt or copy at
00005 // http://www.boost.org/LICENSE_1_0.txt)
00006 //
00007 // Template class numeric_traits<Number> --
00008 //
00009 //    Supplies:
00010 //
00011 //      typedef difference_type -- a type used to represent the difference
00012 //      between any two values of Number.
00013 //
00014 //    Support:
00015 //      1. Not all specializations are supplied
00016 //
00017 //      2. Use of specializations that are not supplied will cause a
00018 //      compile-time error
00019 //
00020 //      3. Users are free to specialize numeric_traits for any type.
00021 //
00022 //      4. Right now, specializations are only supplied for integer types.
00023 //
00024 //      5. On implementations which do not supply compile-time constants in
00025 //      std::numeric_limits<>, only specializations for built-in integer types
00026 //      are supplied.
00027 //
00028 //      6. Handling of numbers whose range of representation is at least as
00029 //      great as boost::intmax_t can cause some differences to be
00030 //      unrepresentable in difference_type:
00031 //
00032 //        Number    difference_type
00033 //        ------    ---------------
00034 //        signed    Number
00035 //        unsigned  intmax_t
00036 //
00037 // template <class Number> typename numeric_traits<Number>::difference_type
00038 // numeric_distance(Number x, Number y)
00039 //    computes (y - x), attempting to avoid overflows.
00040 //
00041 
00042 // See http://www.boost.org for most recent version including documentation.
00043 
00044 // Revision History
00045 // 11 Feb 2001 - Use BOOST_STATIC_CONSTANT (David Abrahams)
00046 // 11 Feb 2001 - Rolled back ineffective Borland-specific code
00047 //               (David Abrahams)
00048 // 10 Feb 2001 - Rolled in supposed Borland fixes from John Maddock, but
00049 //               not seeing any improvement yet (David Abrahams)
00050 // 06 Feb 2001 - Factored if_true out into boost/detail/select_type.hpp
00051 //               (David Abrahams)
00052 // 23 Jan 2001 - Fixed logic of difference_type selection, which was
00053 //               completely wack. In the process, added digit_traits<>
00054 //               to compute the number of digits in intmax_t even when
00055 //               not supplied by numeric_limits<>. (David Abrahams)
00056 // 21 Jan 2001 - Created (David Abrahams)
00057 
00058 #ifndef BOOST_NUMERIC_TRAITS_HPP_DWA20001901
00059 # define BOOST_NUMERIC_TRAITS_HPP_DWA20001901
00060 
00061 # include <boost/config.hpp>
00062 # include <boost/cstdint.hpp>
00063 # include <boost/static_assert.hpp>
00064 # include <boost/type_traits.hpp>
00065 # include <boost/detail/select_type.hpp>
00066 # include <boost/limits.hpp>
00067 
00068 namespace boost { namespace detail {
00069 
00070   // Template class is_signed -- determine whether a numeric type is signed
00071   // Requires that T is constructable from the literals -1 and 0.  Compile-time
00072   // error results if that requirement is not met (and thus signedness is not
00073   // likely to have meaning for that type).
00074   template <class Number>
00075   struct is_signed
00076   {
00077 #if defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) || defined(BOOST_MSVC) && BOOST_MSVC <= 1300
00078     BOOST_STATIC_CONSTANT(bool, value = (Number(-1) < Number(0)));
00079 #else
00080     BOOST_STATIC_CONSTANT(bool, value = std::numeric_limits<Number>::is_signed);
00081 #endif
00082   };
00083 
00084 # ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
00085   // digit_traits - compute the number of digits in a built-in integer
00086   // type. Needed for implementations on which numeric_limits is not specialized
00087   // for intmax_t (e.g. VC6).
00088   template <bool is_specialized> struct digit_traits_select;
00089 
00090   // numeric_limits is specialized; just select that version of digits
00091   template <> struct digit_traits_select<true>
00092   {
00093       template <class T> struct traits
00094       {
00095           BOOST_STATIC_CONSTANT(int, digits = std::numeric_limits<T>::digits);
00096       };
00097   };
00098 
00099   // numeric_limits is not specialized; compute digits from sizeof(T)
00100   template <> struct digit_traits_select<false>
00101   {
00102       template <class T> struct traits
00103       {
00104           BOOST_STATIC_CONSTANT(int, digits = (
00105               sizeof(T) * std::numeric_limits<unsigned char>::digits
00106               - (is_signed<T>::value ? 1 : 0))
00107               );
00108       };
00109   };
00110 
00111   // here's the "usable" template
00112   template <class T> struct digit_traits
00113   {
00114       typedef digit_traits_select<
00115                 ::std::numeric_limits<T>::is_specialized> selector;
00116       typedef typename selector::template traits<T> traits;
00117       BOOST_STATIC_CONSTANT(int, digits = traits::digits);
00118   };
00119 #endif
00120 
00121   // Template class integer_traits<Integer> -- traits of various integer types
00122   // This should probably be rolled into boost::integer_traits one day, but I
00123   // need it to work without <limits>
00124   template <class Integer>
00125   struct integer_traits
00126   {
00127 # ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
00128    private:
00129       typedef Integer integer_type;
00130       typedef std::numeric_limits<integer_type> x;
00131 #   if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
00132       // for some reason, MSVC asserts when it shouldn't unless we make these
00133       // local definitions
00134       BOOST_STATIC_CONSTANT(bool, is_integer = x::is_integer);
00135       BOOST_STATIC_CONSTANT(bool, is_specialized = x::is_specialized);
00136       
00137       BOOST_STATIC_ASSERT(is_integer);
00138       BOOST_STATIC_ASSERT(is_specialized);
00139 #   endif
00140    public:
00141       typedef typename
00142       if_true<(int(x::is_signed)
00143               && (!int(x::is_bounded)
00144                  // digits is the number of no-sign bits
00145                   || (int(x::digits) + 1 >= digit_traits<boost::intmax_t>::digits)))>::template then<
00146         Integer,
00147           
00148       typename if_true<(int(x::digits) + 1 < digit_traits<signed int>::digits)>::template then<
00149         signed int,
00150 
00151       typename if_true<(int(x::digits) + 1 < digit_traits<signed long>::digits)>::template then<
00152         signed long,
00153 
00154    // else
00155         intmax_t
00156       >::type>::type>::type difference_type;
00157 #else
00158       BOOST_STATIC_ASSERT(boost::is_integral<Integer>::value);
00159 
00160       typedef typename
00161       if_true<(sizeof(Integer) >= sizeof(intmax_t))>::template then<
00162                
00163         typename if_true<(is_signed<Integer>::value)>::template then<
00164           Integer,
00165           intmax_t
00166         >::type,
00167 
00168         typename if_true<(sizeof(Integer) < sizeof(std::ptrdiff_t))>::template then<
00169           std::ptrdiff_t,
00170           intmax_t
00171         >::type
00172       >::type difference_type;
00173 # endif
00174   };
00175 
00176   // Right now, only supports integers, but should be expanded.
00177   template <class Number>
00178   struct numeric_traits
00179   {
00180       typedef typename integer_traits<Number>::difference_type difference_type;
00181   };
00182 
00183   template <class Number>
00184   typename numeric_traits<Number>::difference_type numeric_distance(Number x, Number y)
00185   {
00186       typedef typename numeric_traits<Number>::difference_type difference_type;
00187       return difference_type(y) - difference_type(x);
00188   }
00189 }}
00190 
00191 #endif // BOOST_NUMERIC_TRAITS_HPP_DWA20001901


appl
Author(s): petercai
autogenerated on Tue Jan 7 2014 11:02:29