I’m implementing a random number engine myself(No, I’m not inventing one) and want to know what should be done if the parameter is negative. So i check the code of mersenne_twister_engine and found this:
void discard(unsigned long long _Nskip)
{ // discard _Nskip elements
for (; 0 < _Nskip; --_Nskip)
(*this)();
}
Isn’t unsigned type dangerous in these place?
Some people like to use
unsignedfor variables that only store positive quantities. Some other people don’t want to useunsignedto denote that meaning.It’s an often and much discussed topic. I’m in the latter camp: I won’t use
unsignedin such parameters. When I write asize()function for alist-like class for instance, I useint, even though a size will never become smaller than 0.Putting an assert or test-and-throw to reject negative
intvalues seems appropriate if you want. People from theunsignedcamp will say that the compiler should warn on the call-side when you pass a negative value. You can go on with arguments and I’m sure you will find lots of them on the interwebs.