Skip to content

Latest commit

 

History

History
61 lines (47 loc) · 1.89 KB

File metadata and controls

61 lines (47 loc) · 1.89 KB

ckd_add

  • stdckdint.h[meta header]
  • function template[meta id-type]
  • cpp26[meta cpp]
template<class type1, class type2, class type3>
bool ckd_add(type1* result, type2 a, type3 b);

概要

検査付きの整数の加算を行う。a + bを計算し、結果をresultが指す先に格納する。オーバーフローが発生した場合はtrueを返す。

C言語の<stdckdint.h>で定義される関数であり、C++においてはC互換性のために提供される。C言語では型総称マクロ (type-generic macro) として定義されるが、C++では関数テンプレートとして提供される。

テンプレートパラメータ制約

  • type1type2type3はいずれも符号付き整数型または符号なし整数型であること

戻り値

a + bの数学的な結果がtype1で表現可能な範囲を超える場合 (オーバーフロー) はtrueを返し、そうでなければfalseを返す。

効果

a + bの数学的な結果をtype1に変換した値を*resultに格納する。

#include <stdckdint.h>
#include <cassert>
#include <climits>

int main() {
  int result = 0;

  // オーバーフローしない場合
  bool overflow = ckd_add(&result, 1, 2);
  assert(!overflow);
  assert(result == 3);

  // オーバーフローする場合
  overflow = ckd_add(&result, INT_MAX, 1);
  assert(overflow);
}
  • ckd_add[color ff0000]

出力

バージョン

言語

  • C++26

処理系

  • Clang: 21 [mark verified]
  • GCC: 15 [mark verified]
  • Visual C++: 2026 Update 2 [mark noimpl]

参照