- stdckdint.h[meta header]
- function template[meta id-type]
- cpp26[meta cpp]
template<class type1, class type2, class type3>
bool ckd_sub(type1* result, type2 a, type3 b);検査付きの整数の減算を行う。a - bを計算し、結果をresultが指す先に格納する。オーバーフローが発生した場合はtrueを返す。
C言語の<stdckdint.h>で定義される関数であり、C++においてはC互換性のために提供される。C言語では型総称マクロ (type-generic macro) として定義されるが、C++では関数テンプレートとして提供される。
type1、type2、type3はいずれも符号付き整数型または符号なし整数型であること
a - bの数学的な結果がtype1で表現可能な範囲を超える場合 (オーバーフロー) はtrueを返し、そうでなければfalseを返す。
a - bの数学的な結果をtype1に変換した値を*resultに格納する。
#include <stdckdint.h>
#include <cassert>
#include <climits>
int main() {
int result = 0;
// オーバーフローしない場合
bool overflow = ckd_sub(&result, 1, 2);
assert(!overflow);
assert(result == -1);
// オーバーフローする場合
overflow = ckd_sub(&result, INT_MIN, 1);
assert(overflow);
}- ckd_sub[color ff0000]
- C++26
- Clang: 21 [mark verified]
- GCC: 15 [mark verified]
- Visual C++: 2026 Update 2 [mark noimpl]