Boost::integer::integer_traits














































Boost::integer::integer_traits



DESCRIPTION:-
Boost.Integer provides integer type support, particularly helpful in generic programming. It provides the means to select an integer type based upon its properties, like the number of bits or the maximum supported value, as well as compile-time bit mask selection.

Template class integer_traits is derived from std::numeric_limits. The primary specialization adds the single bool member is_integral with the compile-time constant value false. However, for all integral types T (std::3.9.1/7 [basic.fundamental]), there are specializations provided with the following compile-time constants defined:



#################PROGRAM CODE########################

//header file for input output stream
#include <iostream>
//to include required boost header file
#include <boost/integer_traits.hpp>
#include <boost/cstdint.hpp>
#include <boost/detail/lightweight_test.hpp>
#if defined(BOOST_MSVC) && (BOOST_MSVC <= 1300)
template<typename T> inline T make_char_numeric_for_streaming(T x) { return x; }
namespace fix{
inline int make_char_numeric_for_streaming(char c) { return c; }
inline int make_char_numeric_for_streaming(signed char c) { return c; }
inline int make_char_numeric_for_streaming(unsigned char c) { return c; }
}
using namespace fix;
#else
template<typename T> inline T make_char_numeric_for_streaming(T x) { return x; }
inline int make_char_numeric_for_streaming(char c) { return c; }
inline int make_char_numeric_for_streaming(signed char c) { return c; }
inline int make_char_numeric_for_streaming(unsigned char c) { return c; }
#endif

template<class T>
// fuction to check min and max
void runtest(const char * type, T)
{

typedef boost::integer_traits<T> traits;
std::cout << "Checking " << type
<<
"; min is " << make_char_numeric_for_streaming((traits::min)())
<<
", max is " << make_char_numeric_for_streaming((traits::max)())
<<
std::endl;
BOOST_TEST(traits::is_specialized);
#if defined(BOOST_MSVC) && (BOOST_MSVC <= 1200)
// BOOST_TEST instantiation and the preceding one.
BOOST_TEST(traits::is_integer &&
true);
#else
BOOST_TEST(traits::is_integer);
#endif
BOOST_TEST(traits::is_integral ==
true);
BOOST_TEST(traits::const_min == (traits::min)());
BOOST_TEST(traits::const_max == (traits::max)());
}

int main(int, char*[])
{
runtest(
"bool", bool()); //test for bool
runtest(
"char", char()); //test for char
typedef signed char signed_char; //declaring signed char
runtest(
"signed char", signed_char()); //test for signed char
typedef unsigned char unsigned_char; //declaring unsigned char
runtest(
"unsigned char", unsigned_char()); //test for unsigned char
runtest(
"wchar_t", wchar_t()); //test for wide char
runtest(
"short", short()); //test for short
typedef unsigned short unsigned_short; //declaring unsigned short
runtest("unsigned short", unsigned_short()); //test for unsigned short
runtest(
"int", int()); //test for int
typedef unsigned int unsigned_int; //declaring unsigned int
runtest(
"unsigned int", unsigned_int()); //test for unsigned int
runtest(
"long", long()); //test for long
typedef unsigned long unsigned_long; //declaring unsigned long
runtest(
"unsigned long", unsigned_long()); //test for unsigned long
#ifndef BOOST_NO_INTEGRAL_INT64_T
//
// MS/Borland compilers can't support 64-bit member constants
// BeOS doesn't have specialisations for long long in SGI's <limits> header.
runtest(
"int64_t (possibly long long)", boost::int64_t());
runtest(
"uint64_t (possibly unsigned long long)", boost::uint64_t());
#else
std::cout << "Skipped int64_t and uint64_t" << std::endl;
#endif
return boost::report_errors();
}




//############OUTPUT###############//
Checking bool; min is 0, max is 1
Checking char; min is -128, max is 127
Checking signed char; min is -128, max is 127
Checking unsigned char; min is 0, max is 255
Checking wchar_t; min is -2147483648, max is 2147483647
Checking short; min is -32768, max is 32767
No errors detected.
Checking unsigned short; min is 0, max is 65535
Checking int; min is -2147483648, max is 2147483647
Checking unsigned int; min is 0, max is 4294967295
Checking long; min is -9223372036854775808, max is 9223372036854775807
Checking unsigned long; min is 0, max is 18446744073709551615
Checking int64_t (possibly long long); min is -9223372036854775808, max is 9223372036854775807
Checking uint64_t (possibly unsigned long long); min is 0, max is 18446744073709551615