我在使用 gcc4.4 的 Ubuntu 10.04 中遇到同样的问题,相同的代码有效 使用 gcc4.1 在 RH 5.5 上很好
#include <sstream>
#include <iostream>
int main(int argc, char** argv) {
std::stringstream myStream;
myStream << "-123";
unsigned int myUInt;
myStream >> myUInt;
if(myStream.fail()) {
std::cout << "FAILED" << std::endl;
}
}
不给FAILED,这个我已经找到了:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39802
其中声明在 gcc4.1 中已更正,而不是 确保如果错过行为(除非我错过了什么) 与同一问题有关。
最佳答案
我不确定您为什么期望它失败。 sscanf() 也不会失败,但会读取数字,C++ 流应该像 scanf 函数一样工作:
#include <stdio.h>
int main(int argc, char** argv) {
unsigned int n;
if ( ! sscanf( "-1", "%ud", & n ) ) {
printf( "fail\n" );
}
else {
printf( "%ud", n );
}
}
打印 4294967295d。
关于c++ - stringstream 在无符号类型中失败 "streaming"负值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3407100/