ckd_add
|
ヘッダーで定義
<stdckdint.h>
|
||
|
template
<
class
type1,
class
type2,
class
type3
>
bool ckd_add ( type1 * result, type2 a, type3 b ) ; |
(C++26以降) | |
加算 x + y を計算し、結果を * result に格納します。加算は、両オペランドが無限の範囲を持つ符号付き整数型で表現されているものとして実行され、結果はこの整数型から type1 に変換されます。 * result に代入される値が演算の数学的な結果を正しく表す場合、 false を返します。それ以外の場合、 true を返します。この場合、 * result に代入される値は、演算の数学的な結果が * result の幅にラップアラウンドされた値です。
目次パラメータ
戻り値false が、加算の数学的結果が * result に正しく代入された場合を示し、 true がそれ以外の場合を示します。 注記
関数テンプレート
type1 、 type2 、および type3 の各型は、cv修飾されていない符号付きまたは符号なし整数型です。 type2 または type3 が適切な整数型でない場合、あるいは * result が適切な整数型の変更可能な左辺値でない場合、診断メッセージを生成することが推奨されます。 例このコードを実行 #include <cstdint>
#include <limits>
#include <print>
#include <stdckdint.h>
int main()
{
const std::uint8_t x{14};
std::uint16_t y{28}, result1{};
bool overflow{};
overflow = ckd_add(&result1, x, y);
std::println("{} + {} => {} ({})", x, y, result1, overflow ? "Overflow" : "OK");
y = std::numeric_limits<std::uint16_t>::max();
overflow = ckd_add(&result1, x, y);
std::println("{} + {} => {} ({})", x, y, result1, overflow ? "Overflow" : "OK");
std::uint32_t result2{};
overflow = ckd_add(&result2, x, y);
std::println("{} + {} => {} ({})", x, y, result2, overflow ? "Overflow" : "OK");
}
出力: 14 + 28 => 42 (OK)
14 + 65535 => 13 (Overflow)
14 + 65535 => 65549 (OK)
参考文献
関連項目
| ||||||||||||