So I'm trying to understand how exactly the truth value of (stringstream >> int variable) works. Let the code be something like this:
int number;
stringstream convert("string");
if (convert >> number) {do something;}
Could someone please explain to me when exactly does (convert >> number) evaluate to true and when to false, since it is not identical to successful initialisation of an integer variable. For clarification, I know why it works, it works because it evaluates to a bool
. The question is how exactly does it work. This question does not deal with the same problem.
I've seen similar questions, but they don't address exactly this problem. During testing I found some ways in which it's different from normal initialization of an int variable. For example:
int number = 2147483648;
causes an overflow, but the assignment is successful nonetheless. On the other hand
int number;
stringstream convert("2147483648");
(convert >> number)
evaluates to false. It is not identical to the "correct" initialisation either. For example
unsigned int number;
stringstream convert("2147483648");
(convert >> number)
evaluates to true
, as expected, but
unsigned int number;
stringstream convert("-1");
(convert >> number)
evaluates to true also, even though unsigned int
expects a positive value.
What else is different? Thank you.