首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::numeric_limits::is_modulo

static const bool is_modulo;

?

(until C++11)

static constexpr bool is_modulo;

?

(since C++11)

价值std::numeric_limits<T>::is_modulotrue所有算术类型T该句柄溢出了模运算,也就是说,如果这种类型的加法、减法、乘法或除法的结果超出了范围。[min(), max()],则此操作返回的值与预期值的倍数有所不同。max()-min()+1...

标准专业化

T

value of std::numeric_limits<T>::is_modulo

/* non-specialized */

false

bool

false

char

implementation-defined

signed char

implementation-defined

unsigned char

true

wchar_t

implementation-defined

char16_t

implementation-defined

char32_t

implementation-defined

short

implementation-defined

unsigned short

true

int

implementation-defined

unsigned int

true

long

implementation-defined

unsigned long

true

long long

implementation-defined

unsigned long long

true

float

false

double

false

long double

false

注记

虽然C++11标准仍然说:“在大多数机器上,有符号整数都是这样。”但是,确切的措辞从C++03改为C++11,其方式是true值不再兼容有符号整数溢出的未定义行为%28它从“可能”改为严格要求%29。正因为如此,现在设置了依赖于签名溢出的实现(%28)以获得优化机会%29。is_modulofalse有符号整数。例如见gcc PR 22200...

预计标准措辞将改为“is”。[医]模对于有符号整数类型是false,除非实现定义有符号整数溢出以包装“per”。lwg第2422期...

演示模块类型的行为。

二次

代码语言:javascript
复制
#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 << "\nmax 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>();
    check_overflow<unsigned long>();
    // check_overflow<float>(); // compile-time error, not a modulo type
}

二次

可能的产出:

二次

代码语言:javascript
复制
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

二次

另见

is_integer static

identifies integer types (public static member constant)

is_iec559 static

identifies the IEC 559/IEEE 754 floating-point types (public static member constant)

is_exact static

identifies exact types (public static member constant)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com