最終更新日時(UTC):
が更新

履歴 編集

function template
<stdckdint.h>

ckd_mul(C++26)

template<class type1, class type2, class type3>
bool ckd_mul(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_mul(&result, 3, 4);
  assert(!overflow);
  assert(result == 12);

  // オーバーフローする場合
  overflow = ckd_mul(&result, INT_MAX, 2);
  assert(overflow);
}

出力

バージョン

言語

  • C++26

処理系

参照