Previous fileTop of DocumentContentsIndex pageNext file
Apache C++ Standard Library User's Guide

21.1 Overview

A feature of the C++ Standard Library is an organized mechanism for describing the characteristics of the fundamental types provided in the execution environment. In older C and C++ libraries, these characteristics were often described by large collections of symbolic constants or macros. For example, the smallest representable value that could be stored in a variable of type char would be found in the constant named CHAR_MIN; the similar constant for a short would be known as SHRT_MIN; a float would be FLT_MIN, and so on. These manifest constants are still available in the headers <climits> and <cfloat>.

The class template numeric_limits provides a uniform way of representing this information for all numeric types. Instead of using a different symbolic name for each new datatype, the primary class template defines a generic interface in the form of static data members and static member functions. The primary template supplies useless values just so that it can still be used in generic code. It can be distinguished from its specializations by examining the value of the static const bool data member std::numeric_limits<>::is_specialized which will be false for the primary template and true for any specialization. Specializations of this class template then provide a meaningful definition of every member for each fundamental type. For example, the smallest character value is found as the result of invoking the function std::numeric_limits<char>::min(), while the smallest floating point value is found using std::numeric_limits<float>::min(), and so on.

Using a class template not only greatly reduces the number of symbolic names that need to be defined to describe the operating environment, but it also ensures consistency between the descriptions of the various types. Most important, however, it allows programs to treat any object or variable generically, without regard to its specific type.

For the sake of compatibility, the numeric_limits mechanism is used as an addition to the symbolic constants used in older C++ libraries, rather than a strict replacement. Thus both mechanisms exist in parallel for the present. The library provides specializations of numeric_limits for all fundamental types including bool and wchar_t. Users are expected to provide specializations for their own types, such as rational.

Previous fileTop of DocumentContentsIndex pageNext file