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 の幅にラップアラウンドされた値です。
目次 |
パラメータ
| a, b | - | 整数値 |
| result | - | 結果を格納するアドレス |
戻り値
false が、加算の数学的結果が * result に正しく代入された場合を示し、 true がそれ以外の場合を示します。
注記
関数テンプレート
ckd_add
は、
型総称マクロ
と同じ名前で
C23
で規定されている対応するものと同じセマンティクスを持ちます。
type1 、 type2 、および type3 の各型は、cv修飾されていない符号付きまたは符号なし整数型です。
type2 または type3 が適切な整数型でない場合、あるいは * result が適切な整数型の変更可能な左辺値でない場合、診断メッセージを生成することが推奨されます。
例
Compiler Explorer プレビュー .
#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)
参考文献
- C++26標準 (ISO/IEC 14882:2026):
-
- 29.11.2 チェック済み整数演算
関連項目
|
(C++26)
|
2つの整数に対するチェック付き減算演算
(関数テンプレート) |
|
(C++26)
|
2つの整数に対するチェック付き乗算演算
(関数テンプレート) |
|
Cドキュメント
for
ckd_add
|
|