Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

iostream

I want to read unsigned integers in base-10 (decimal) representation from a C++ iostream with at least rudimentary error detection. In my view, minus signs would clearly be an error in this case, because unsigned integers have no sign. However, the gcc is of a different opinion:

#include <iostream>
#include <sstream>

int main() {
    std::stringstream a("5"), b("-0"), c("-4");
    unsigned int i;
    a >> i; if ( a ) std::cout << i << std::endl; else std::cout << "Conversion failure" << std::endl;
    b >> i; if ( b ) std::cout << i << std::endl; else std::cout << "Conversion failure" << std::endl;
    c >> i; if ( c ) std::cout << i << std::endl; else std::cout << "Conversion failure" << std::endl;
    return 0;
}

gives me an output of

4294967292

for the last line, as if a signed integer -4 had been read and converted to unsigned int.

Apparently, the GCC people see this as a feature. Is there some standard that mandates this behaviour, and is there any way short of writing an own parser to get out of it, i.e. detect "-4" (and maybe "-0") as conversion errors?

like image 716
thiton Avatar asked Oct 06 '11 16:10

thiton


People also ask

What happens if you pass negative number as unsigned int?

You simply cannot assign a negative value to an object of an unsigned type. Any such value will be converted to the unsigned type before it's assigned, and the result will always be >= 0.

How do you check if an unsigned int is negative?

An unsigned integer can never hold a negative value. If the value of i is negative, then it will be converted to some positive value when you assign it to j . If you want to know whether the value of i is negative, you should test i , not j . Note that converting the value of j back to int is not reliable.

Does Sscanf work with negative numbers?

You can scan a negative number with scanf("%u", &number); . It will be negated in the destination type, namely unsigned int , with the same bitwise representation as the negative number in a signed int , for two's complement representation which is almost universal on current architectures.


1 Answers

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>>.

like image 169
Steve Jessop Avatar answered Sep 29 '22 18:09

Steve Jessop