Pigweed
C/C++ API Reference
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Loading...
Searching...
No Matches
Functions
Pw_numeric_checked_arithmetic

Functions

template<typename A , typename B , typename T >
constexpr bool pw::CheckedAdd (A a, B b, T &result)
 
template<typename T , typename A , typename B >
constexpr std::optional< T > pw::CheckedAdd (A a, B b)
 
template<typename T , typename Inc >
constexpr bool pw::CheckedIncrement (T &base, Inc inc)
 
template<typename A , typename B , typename T >
constexpr bool pw::CheckedSub (A a, B b, T &result)
 
template<typename T , typename A , typename B >
constexpr std::optional< T > pw::CheckedSub (A a, B b)
 
template<typename T , typename Dec >
constexpr bool pw::CheckedDecrement (T &base, Dec dec)
 
template<typename A , typename B , typename T >
constexpr bool pw::CheckedMul (A a, B b, T &result)
 
template<typename T , typename A , typename B >
constexpr std::optional< T > pw::CheckedMul (A a, B b)
 

Detailed Description

Function Documentation

◆ CheckedAdd() [1/2]

template<typename T , typename A , typename B >
constexpr std::optional< T > pw::CheckedAdd ( a,
b 
)
constexpr

Adds two numbers, checking for overflow.

Template Parameters
TThe type of the result, which is checked for overflow.
AThe type of the first addend, a.
BThe type of the second addend, b.
Parameters
[in]aThe first addend.
[in]bThe second addend.
Returns
The sum (a + b) if addition was successful, or nullopt if the addition would overflow.
Note
The template argument must be provided, e.g. pw::CheckedAdd<uint32_t>(...).

◆ CheckedAdd() [2/2]

template<typename A , typename B , typename T >
constexpr bool pw::CheckedAdd ( a,
b,
T &  result 
)
constexpr

Adds two numbers, checking for overflow.

Template Parameters
AThe type of the first addend, a.
BThe type of the second addend, b.
TThe type of the result, which is checked for overflow.
Parameters
[in]aThe first addend.
[in]bThe second addend.
[out]resultReference to t
Returns
The sum (a + b) if addition was successful, or false if the addition would overflow and result is unmodified.
Note
The template argument must be provided, e.g. pw::CheckedAdd<uint32_t>(...).

◆ CheckedDecrement()

template<typename T , typename Dec >
constexpr bool pw::CheckedDecrement ( T &  base,
Dec  dec 
)
constexpr

Decrements a variable by some amount.

Template Parameters
TThe type of the variable to be decremented.
DecThe type of the variable to subtract.
Parameters
[in]baseThe variable to be decremented.
[in]decThe number to subtract from base.
Returns
True if the subtraction was successful and base was decremented (base -= dec); False if the subtraction would overflow and base is unmodified.

◆ CheckedIncrement()

template<typename T , typename Inc >
constexpr bool pw::CheckedIncrement ( T &  base,
Inc  inc 
)
constexpr

Increments a variable by some amount.

Template Parameters
TThe type of the variable to be incremented.
IncThe type of the variable to add.
Parameters
[in]baseThe variable to be incremented.
[in]incThe number to add to base.
Returns
True if the addition was successful and base was incremented (base += inc); False if the addition would overflow and base is unmodified.

◆ CheckedMul() [1/2]

template<typename T , typename A , typename B >
constexpr std::optional< T > pw::CheckedMul ( a,
b 
)
constexpr

Multiplies two numbers, checking for overflow.

Template Parameters
TThe type of the result, which is checked for overflow.
AThe type of the first factor, a.
BThe type of the second factor, b.
Parameters
[in]aThe first factor.
[in]bThe second factor.
Returns
The product (a * b) if multiplication was successful, or nullopt if the multiplication would overflow.
Note
The template argument must be provided, e.g. pw::CheckedMul<uint32_t>(...).

◆ CheckedMul() [2/2]

template<typename A , typename B , typename T >
constexpr bool pw::CheckedMul ( a,
b,
T &  result 
)
constexpr

Multiplies two numbers, checking for overflow.

Template Parameters
AThe type of the first addend, a.
BThe type of the second addend, b.
TThe type of the result, which is checked for overflow.
Parameters
[in]aThe first addend.
[in]bThe second addend.
[out]resultReference to the result.
Returns
true if the multiplication was successful, false if it would overflow.
Note
The template argument must be provided, e.g. pw::CheckedMul<uint32_t>(...).

◆ CheckedSub() [1/2]

template<typename T , typename A , typename B >
constexpr std::optional< T > pw::CheckedSub ( a,
b 
)
constexpr

Subtracts two numbers, checking for overflow.

Template Parameters
TThe type of the result, which is checked for overflow.
AThe type of the minuend, a.
BThe type of the subtrahend, b.
Parameters
[in]aThe minuend (the number from which b is subtracted).
[in]bThe subtrahend (the number subtracted from a).
Returns
The difference (a - b) if subtraction was successful, or nullopt if the subtraction would overflow.
Note
The template argument must be provided, e.g. pw::CheckedSub<uint32_t>(...).

◆ CheckedSub() [2/2]

template<typename A , typename B , typename T >
constexpr bool pw::CheckedSub ( a,
b,
T &  result 
)
constexpr

Subtracts two numbers, checking for overflow.

Template Parameters
AThe type of the first addend, a.
BThe type of the second addend, b.
TThe type of the result, which is checked for overflow.
Parameters
[in]aThe first addend.
[in]bThe second addend.
[out]resultReference to the result.
Returns
true if the subtraction was successful, false if it would overflow and result is unmodified.
Note
The template argument must be provided, e.g. pw::CheckedSub<uint32_t>(...).