Files CCore/inc/gadget/PosSub.h CCore/src/gadget/PosSub.cpp
PosSub() is a function, performing the positive subtraction:
// pseudocode if( a>=b ) PosSub(a,b) == a-b ; if( a<b ) PosSub(a,b) == 0 ;
template <class UInt,class ... TT>
PosSubResult<UInt> PosSub(UInt value,TT ... tt);
PosSub() accepts any number of arguments, value type is determined from the first one, it must be an unsigned integral type, other arguments must be convertible to this type. Extra arguments are subtracted one by one from the first one.
The resulting type encodes both the result and the no-underflow flag, it can be implicitly converted to the result.
template <class UInt>
struct PosSubResult
{
UInt value;
bool ok;
operator UInt() const { return value; }
....
};
Some examples:
ulen len=PosSub(total_len,prefix1,prefix2);
auto len=PosSub(total_len,prefix1,prefix2); if( len.ok ) { // do job } else { // handle failure }