C++ std::numeric_limits

一 简介

头文件

template  numeric_limits;

Provides information about the properties of arithmetic types (either integral or floating-point) in the specific platform for which the library compiles.

This class template is specialized for every fundamental arithmetic type, with its members describing the properties of type T. This template shall not be specialized for any other type.

 即:提供算术类型(整型或者浮点型)的属性信息。该类模板提供了每个基本算术类型的特化,类成员描述类型的属性,成员和成员函数均为静态;而其他类型并没有进行特化,将使用默认的模板定义

二 基本算术类型

C++98:

fundamental arithmetic types
integral types bool
char
wchar_t
signed char
short int
int
long int
unsigned char
unsigned short int
unsigned int
unsigned long int
floating point types float
double
long double

C++11:
 

fundamental arithmetic types
integral types bool
char
char16_t
char32_t
wchar_t
signed char
short int
int
long int
long long int
unsigned char
unsigned short int
unsigned int
unsigned long int
unsigned long long int
floating point types float
double
long double

三 默认模板定义

C++98:

template  class numeric_limits {
public:
  static const bool is_specialized = false;
  static T min() throw();
  static T max() throw();
  static const int  digits = 0;
  static const int  digits10 = 0;
  static const bool is_signed = false;
  static const bool is_integer = false;
  static const bool is_exact = false;
  static const int radix = 0;
  static T epsilon() throw();
  static T round_error() throw();

  static const int  min_exponent = 0;
  static const int  min_exponent10 = 0;
  static const int  max_exponent = 0;
  static const int  max_exponent10 = 0;

  static const bool has_infinity = false;
  static const bool has_quiet_NaN = false;
  static const bool has_signaling_NaN = false;
  static const float_denorm_style has_denorm = denorm_absent;
  static const bool has_denorm_loss = false;
  static T infinity() throw();
  static T quiet_NaN() throw();
  static T signaling_NaN() throw();
  static T denorm_min() throw();

  static const bool is_iec559 = false;
  static const bool is_bounded = false;
  static const bool is_modulo = false;

  static const bool traps = false;
  static const bool tinyness_before = false;
  static const float_round_style round_style = round_toward_zero;
};

C++11:

template  class numeric_limits {
public:
  static constexpr bool is_specialized = false;
  static constexpr T min() noexcept { return T(); }
  static constexpr T max() noexcept { return T(); }
  static constexpr T lowest() noexcept { return T(); }
  static constexpr int  digits = 0;
  static constexpr int  digits10 = 0;
  static constexpr bool is_signed = false;
  static constexpr bool is_integer = false;
  static constexpr bool is_exact = false;
  static constexpr int radix = 0;
  static constexpr T epsilon() noexcept { return T(); }
  static constexpr T round_error() noexcept { return T(); }

  static constexpr int  min_exponent = 0;
  static constexpr int  min_exponent10 = 0;
  static constexpr int  max_exponent = 0;
  static constexpr int  max_exponent10 = 0;

  static constexpr bool has_infinity = false;
  static constexpr bool has_quiet_NaN = false;
  static constexpr bool has_signaling_NaN = false;
  static constexpr float_denorm_style has_denorm = denorm_absent;
  static constexpr bool has_denorm_loss = false;
  static constexpr T infinity() noexcept { return T(); }
  static constexpr T quiet_NaN() noexcept { return T(); }
  static constexpr T signaling_NaN() noexcept { return T(); }
  static constexpr T denorm_min() noexcept { return T(); }

  static constexpr bool is_iec559 = false;
  static constexpr bool is_bounded = false;
  static constexpr bool is_modulo = false;

  static constexpr bool traps = false;
  static constexpr bool tinyness_before = false;
  static constexpr float_round_style round_style = round_toward_zero;
};

四 举例

#include 
#include 

int main() {
  std::cout << "type\tlowest()\tmin()\t\tmax()\n\n";

  std::cout << "uchar\t"
            << +std::numeric_limits::lowest() << '\t' << '\t'
            << +std::numeric_limits::min() << '\t' << '\t'
            << +std::numeric_limits::max() << '\n';
  std::cout << "int\t"
            << std::numeric_limits::lowest() << '\t'
            << std::numeric_limits::min() << '\t'
            << std::numeric_limits::max() << '\n';
  std::cout << "float\t"
            << std::numeric_limits::lowest() << '\t'
            << std::numeric_limits::min() << '\t'
            << std::numeric_limits::max() << '\n';
  std::cout << "double\t"
            << std::numeric_limits::lowest() << '\t'
            << std::numeric_limits::min() << '\t'
            << std::numeric_limits::max() << '\n';

  getchar();
}

结果:

C++ std::numeric_limits_第1张图片

五 参考

std::numeric_limits 

你可能感兴趣的:(#,C++98/03)