ckd_sub
|
定义于头文件
<stdckdint.h>
|
||
|
template
<
class
type1,
class
type2,
class
type3
>
bool ckd_sub ( type1 * result, type2 a, type3 b ) ; |
(C++26 起) | |
计算减法运算 x - y 并将结果存储到 * result 中。该减法运算的执行方式如同两个操作数都以具有无限范围的有符号整数类型表示,然后将结果从此整数类型转换为 type1 。如果赋值给 * result 的值正确表示该运算的数学结果,则返回 false 。否则返回 true 。此时赋值给 * result 的值是运算数学结果环绕至 * result 位宽后的值。
内容参数
返回值若赋值给 * result 的值正确表示减法运算的数学结果,则返回 false ;否则返回 true 。 说明
函数模板
类型 type1 、 type2 和 type3 中的每一个都是无 cv 限定的有符号或无符号整数类型。 如果 type2 或 type3 不是合适的整数类型,或者如果 * result 不是合适整数类型的可修改左值,建议生成诊断信息。 示例运行此代码 #include <cstdint>
#include <limits>
#include <print>
#include <stdckdint.h>
int main()
{
int16_t result1{};
constexpr std::int8_t x1{-2};
constexpr std::int16_t y1{1};
const bool underflow1{ckd_sub(&result1, x1, y1)};
std::println("{} - {} == {} ({})", x1, y1, result1, underflow1 ? "Underflow" : "OK");
int16_t result2{};
constexpr std::int8_t x2{-2};
constexpr std::int16_t y2{std::numeric_limits<std::int16_t>::max()};
const bool underflow2{ckd_sub(&result2, x2, y2)};
std::println("{} - {} == {} ({})", x2, y2, result2, underflow2 ? "Underflow" : "OK");
int32_t result3{};
constexpr std::int8_t x3{-2};
constexpr std::int16_t y3{std::numeric_limits<std::int16_t>::max()};
const bool underflow3{ckd_sub(&result3, x3, y3)};
std::println("{} - {} == {} ({})", x3, y3, result3, underflow3 ? "Underflow" : "OK");
}
输出: -2 - 1 == -3 (OK)
-2 - 32767 == 32767 (Underflow)
-2 - 32767 == -32769 (OK)
参考文献
参考
| ||||||||||||