How to detect negative numbers as parsing errors when reading unsigned integers?

Consulting C++03, 22.2.2.1.2/11, the formats are inherited from scanf and friends, which in turn all say that the converted character sequence is “optionally signed”, even for the ones with unsigned output. strtoul is the same.

So, I suppose you could say the standard that mandates the behavior is C89 for C++03, C99 for C++11.

Since the - happens to be allowed only as the first character, I suppose that the workaround is to check for it with peek before using operator>>.

Leave a Comment