You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/modules/ROOT/pages/signed_integers.adoc
+88Lines changed: 88 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -148,3 +148,91 @@ auto b = i32{200};
148
148
// auto result = a + b; // Compile error: mismatched types
149
149
auto result = static_cast<i32>(a) + b; // OK: explicit promotion
150
150
----
151
+
152
+
== Policy-Based Arithmetic Functions
153
+
154
+
In addition to the default operators (which throw on overflow/underflow), signed integer types support the same policy-based free functions as unsigned types.
155
+
See xref:policies.adoc[] for a detailed description of each overflow policy.
156
+
157
+
=== Saturating Arithmetic
158
+
159
+
Saturating operations clamp the result to `std::numeric_limits<T>::max()` on overflow or `std::numeric_limits<T>::min()` on underflow, rather than throwing.
160
+
161
+
[source,c++]
162
+
----
163
+
template <SignedLibType T> constexpr T saturating_add(T lhs, T rhs) noexcept;
164
+
template <SignedLibType T> constexpr T saturating_sub(T lhs, T rhs) noexcept;
165
+
template <SignedLibType T> constexpr T saturating_mul(T lhs, T rhs) noexcept;
166
+
template <SignedLibType T> constexpr T saturating_div(T lhs, T rhs); // throws on div-by-zero
167
+
template <SignedLibType T> constexpr T saturating_mod(T lhs, T rhs); // throws on mod-by-zero
168
+
----
169
+
170
+
NOTE: Division by zero always throws `std::domain_error`, even with the saturating policy. The `min / -1` overflow case saturates to `max()`. The `min % -1` case returns `0`.
171
+
172
+
=== Overflowing Arithmetic
173
+
174
+
Overflowing operations return a `std::pair<T, bool>` where the bool indicates whether overflow or underflow occurred. The result wraps on overflow (two's complement behavior).
0 commit comments