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

人走茶凉 提交于 2019-11-26 17:12:34

问题


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?


回答1:


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




回答2:


If I read 22.2.2.1.2/table 5 correctly it shows that extracting into an unsigned is equivalent to scanf with %u which appears to also do the negative -> positive conversion.




回答3:


Would it be really different if you'd done this?

int i;
unsigned int u;
c >> i;
u = i;
std :: cout << u;

It doesn't matter so much that operator>> tolerates the sign mismatch because the underlying C rules will allow a silent conversion in any case. You're not fundamentally adding any safety by "strengthening" the input operator.

That said, my gcc (4.3.5 on Solaris) says it's a conversion error.



来源:https://stackoverflow.com/questions/7677158/how-to-detect-negative-numbers-as-parsing-errors-when-reading-unsigned-integers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!