fork(1) download
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <cassert>
  5.  
  6. int main()
  7. {
  8. {
  9.  
  10. std::istringstream stream( "-5" );
  11. unsigned int val = 0;
  12. // why is this allowed?
  13. // -5 is not unsigned!
  14. stream >> std::noskipws >> val;
  15. bool success = stream.eof() && !stream.fail();
  16. std::cout << success << ", val: " << val << std::endl;
  17. }
  18.  
  19.  
  20. {
  21. // this still converts the value, but with fail
  22. // you can detect the truncation
  23. std::istringstream stream( "4294967291" );
  24. int val = 0;
  25. stream >> std::noskipws >> val;
  26. bool success = stream.eof() && !stream.fail();
  27. std::cout << success << ", val: " << val << std::endl;
  28. }
  29.  
  30.  
  31. return 0;
  32. }
  33.  
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
1, val: 4294967291
0, val: 2147483647