Compile-Time Integral Type LimitsThe C++ Standard Library <limits> header supplies a class template numeric_limits<> with specializations for each fundamental type. For integer types, the interesting members of std::numeric_limits<> are: static const bool is_specialized; // will be true for integers static T min() throw(); static T max() throw(); static const int digits; // for integers, # value bits static const int digits10; static const bool is_signed; static const bool is_integer; // will be true for integersFor many uses, these are sufficient. But min() and max() are problematical because they are not constant expressions (std::5.19), yet some usages require constant expressions.
The template class Header
|
member | type | value |
---|---|---|
is_integral | bool | true |
const_min | T | equivalent
to std::numeric_limits<T>::min() |
const_max | T | equivalent
to std::numeric_limits<T>::max() |
Note: A flag is_integral
is provided, because a
user-defined integer class should specialize
std::numeric_limits<>::is_integer = true
,
nonetheless compile-time constants const_min
and
const_max
cannot be provided for that user-defined class.
The program integer_traits_test.cpp
exercises the integer_traits
class.
Revised 06 November 2007
? Copyright Beman Dawes 2000
Distributed under the Boost Software License, Version 1.0. See www.boost.org/LICENSE_1_0.txt