Common mathematical functions | |||||||||||||||||||||||||||||||
Mathematical special functions(C++17) | |||||||||||||||||||||||||||||||
Mathematical constants(C++20) | |||||||||||||||||||||||||||||||
Basic linear algebra algorithms(C++26) | |||||||||||||||||||||||||||||||
Data-parallel types (SIMD)(C++26) | |||||||||||||||||||||||||||||||
Floating-point environment(C++11) | |||||||||||||||||||||||||||||||
Complex numbers | |||||||||||||||||||||||||||||||
Numeric array (valarray ) | |||||||||||||||||||||||||||||||
Pseudo-random number generation | |||||||||||||||||||||||||||||||
Bit manipulation(C++20) | |||||||||||||||||||||||||||||||
Saturation arithmetic(C++26) | |||||||||||||||||||||||||||||||
Factor operations | |||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||
Interpolations | |||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||
Generic numeric operations | |||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||
C-style checked integer arithmetic | |||||||||||||||||||||||||||||||
|
|
Defined in header <stdckdint.h> | ||
template<class type1,class type2,class type3> bool ckd_add( type1* result, type2 a, type3 b); | (since C++26) | |
Computes the additionx+ y and stores the result into*result. The addition is performed as if both operands were represented in a signed integer type with infinite range, and the result was then converted from this integer type totype1. If the value assigned to*result correctly represents the mathematical result of the operation, it returnsfalse. Otherwise, it returnstrue. In this case, the value assigned to*result is the mathematical result of the operation wrapped around to the width of*result.
Contents |
a, b | - | integer values |
result | - | address of where result should be stored |
false if the value assigned to*result correctly represents the mathematical result of the addition,true otherwise.
The function templateckd_add
has the same semantics as the correspondingtype-generic macro with the same name specified inC23.
Each of the typestype1,type2, andtype3 is a cv-unqualified signed or unsigned integer type.
It is recommended to produce a diagnostic message iftype2 ortype3 are not suitable integer types, or if*result is not a modifiable lvalue of a suitable integer type.
Compiler Explorerpreview.
#include <cstdint>#include <limits>#include <print>#include <stdckdint.h> int main(){conststd::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");}
Possible output:
14 + 28 => 42 (OK)14 + 65535 => 13 (Overflow)14 + 65535 => 65549 (OK)
(C++26) | checked subtraction operation on two integers (function template)[edit] |
(C++26) | checked multiplication operation on two integers (function template)[edit] |
C documentation forckd_add |