Namespaces
Variants

std::numeric_limits<T>:: is_modulo

From cppreference.net
Utilities library
static const bool is_modulo ;
(C++11 前)
static constexpr bool is_modulo ;
(C++11 起)

对于所有使用模算术处理溢出的算术类型 T std:: numeric_limits < T > :: is_modulo 的值为 true 。具体而言,若该类型的加法、减法、乘法或除法运算结果超出范围 [ min() , max() ] ,则此类操作返回的值与期望值相差 max ( ) - min ( ) + 1 的整数倍。

对于有符号整数类型, is_modulo false ,除非实现定义了有符号整数溢出为回绕行为。

目录

标准特化

T std:: numeric_limits < T > :: is_modulo 的值
/* non-specialized */ false
bool false
char 由实现定义
signed char 由实现定义
unsigned char true
wchar_t 由实现定义
char8_t (C++20 起) true
char16_t (C++11 起) true
char32_t (C++11 起) true
short 由实现定义
unsigned short true
int 由实现定义
unsigned int true
long 由实现定义
unsigned long true
long long (C++11) 由实现定义
unsigned long long (C++11) true
float false
double false
long double false

注释

标准在 LWG issue 2422 决议前曾表述为“在大多数机器上,这对有符号整数成立”。相关讨论可参阅 GCC PR 22200

示例

演示模类型的行为:

#include <iostream>
#include <type_traits>
#include <limits>
template<class T>
typename std::enable_if<std::numeric_limits<T>::is_modulo>::type
    check_overflow()
{
    std::cout << "max value is " << std::numeric_limits<T>::max() << '\n'
              << "min value is " << std::numeric_limits<T>::min() << '\n'
              << "max value + 1 is " << std::numeric_limits<T>::max()+1 << '\n';
}
int main()
{
    check_overflow<int>();
    std::cout << '\n';
    check_overflow<unsigned long>();
//  check_overflow<float>(); // compile-time error, not a modulo type
}

可能的输出:

max value is 2147483647
min value is -2147483648
max value + 1 is -2147483648
max value is 18446744073709551615
min value is 0
max value + 1 is 0

缺陷报告

以下行为变更缺陷报告被追溯应用于先前发布的C++标准。

缺陷报告 适用范围 发布时行为 正确行为
LWG 612 C++98 “以模运算处理溢出”的定义存在缺陷 [1] 提供了
更完善的定义
LWG 2422 C++98 is_modulo 被要求对多数机器上的
有符号整数类型返回 true
改为要求对有符号整数类型返回 false
除非有符号整数溢出被定义为环绕行为
  1. 该定义为“两个正数相加可能产生环绕至第三个更小数值的结果”。存在以下问题:
    • 未定义环绕后的具体数值
    • 未声明结果是否具有可重复性
    • 未要求对所有数值执行加减法及其他运算时均需具备明确的行为定义

参见

[static]
标识整数类型
(公开静态成员常量)
[static]
标识符合 IEC 559/IEEE 754 标准的浮点类型
(公开静态成员常量)
[static]
标识精确类型
(公开静态成员常量)