Alf P. Steinbach wrote:
* Raymond:
>Source:
http://moryton.blogspot.com/2007/08/...flow-when.html
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.
Yes, correct, but in my haste I left out *why* I felt unsure/asked these
questions, and I think I found the part that I had read before; section
1.9 15, where overflows triggering exceptions are mentioned with regards
to integers, or signed integers if the example is to be taken literally.
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
Quite possibly there are existing such classes freely available on the
net -- Google (and please report results of that search here! :-) ).
There is only so much time one can spend searching before beginning to
think again..
Anyway, thanks, but I think you made this a lot more complex than it
needed to be, at least for me. Existing classes? Perhaps, but for such
a small problem, that doesn't sound like the solution for me.
It wasn't easy for me to find something concrete on this subject, and I
tried finding other solutions after this one. The paper on Stroustrup's
page, also linked to from the article, only dealt with signed integers,
as your example did.