* Raymond:
> Source:
> http://moryton.blogspot.com/2007/08/...flow-when.html
>
> Example from source:
>
> char unsigned augend (255);
> char unsigned const addend (255);
> char unsigned const sum (augend + addend);
>
> if (sum < augend)
> {
> std:
uts ("Overflowed!");
> }
>
> sum = augend + addend
> sum = 255 + 255
> sum = 510 modulo 256 // Behind the scenes.
> sum = 254
>
> Does it touch any implementation defined or undefined behaviour, or was
> that specific to signed integers (on some platforms)?
In C++ unsigned integer arithmetic is defined as modulo 2^n, where n is
the number of bits.
> What other methods are there for detecting unsigned integer overflow
> and/or underflow in C++?
Generally you don't.
If you want range checking you can check if your compiler provides range
checking for signed integer types, or you can implement a range-checked
integer type as a class, like
class CheckedInt
{
private:
int myValue;
public:
CheckedInt( int v = 0 ): myValue( v ) {}
int intValue() const { return myValue; }
CheckedInt operator+( CheckedInt const& other ) const
{
int const a = intValue();
int const b = other.intValue();
int const result = a + b;
// Assuming two's complement form:
if( (a < 0) == (b < 0) && (result < 0) != (a < 0) )
{
throw std::something_blah_blah();
}
return result;
}
};
Quite possibly there are existing such classes freely available on the
net -- Google (and please report results of that search here!

).
Cheers, & hth.,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?