Currently I'm self-learning C++ Primer 5th. Here comes something I'm not sure. (I couldn't find the exact relevant question on F.A.Q).
Consider this while loop:
while(std::cin>>value){...} \\value here was defined as int.
The text book says:
That expression reads the next number from the standard input and stores that number in value. The input operator (§ 1.2, p. 8) returns its left operand, which in this case is std::cin. This condition, therefore, tests std::cin.When we use an istream as a condition, the effect is to test the state of the stream. If the stream is valid—that is, if the stream hasn’t encountered an error—then the test succeeds.
My question is: does std::cin
read input into value
first then test the validation of std::cin
, or test std::cin
first then decide whether to read into 'value'? I'm quite confused about when it "returns its left operand".
Remember that your code is equivalent to:
while (std::cin.operator>>(value)) { }
Or:
while (1) {
std::cin >> value ;
if (!std::cin) break ;
}
The "code" always tries to read from std::cin
into value
before testing std::cin
.
Let's look at the quote:
[...] The input operator (§ 1.2, p. 8) returns its left operand, which in this case is std::cin. [...]
This only means that std::cin.operator>>(value)
return std::cin
.
This condition, therefore, tests std::cin. When we use an istream as a condition, the effect is to test the state of the stream. If the stream is valid—that is, if the stream hasn’t encountered an error—then the test succeeds.
What the text book says is that after trying to read an integer from std::cin
to value
, the >>
operator returns std::cin
. If std::cin
is in a good state after reading value
, then the test passes, otherwize it fails.
Some extra details:
When you do std::cin >> value
, you basically call istream::operator>>(int&)
, and yes there is a test inside that method: If the test passes, then the internal state of std::cin
is set to ios_base::goodbit
, if it fails, internal state is set to on of the error flag (eofbit
, failbit
or badbit
).
Depending on the exception mask
for std::cin
, if the internal test fails, an exception may be thrown.
From your quote:
When we use an istream as a condition, the effect is to test the state of the stream.
This basically mean that:
if (std::cin) { }
Is equivalent to:
if (!std::cin.fail()) { }
And std::cin.fail()
check for failbit
or badbit
. This means that while (std::cin >> value) { }
does not test the eofbit
flag and will only fail when the input cannot be converted to an integer value.
does std::cin read input into value first then test the validation of std::cin, or test std::cin first then decide whether to read into 'value'
cin
first tries to read an int
from the standard input, if cin
is in a good state: if it fails to, it will set the stream to a bad state; regardless of the operation done, it will return the stream itself (i.e. the "left operand" -- cin
), that will allow you to check for success or failure.
If you wanted to explicitly test the validity of the stream first and only then try to read the value, you would have:
while (cin && cin >> value)
but it's pretty redundant, since, as I've told you, cin
will not even try to read value
if it's already in a bad state.
There are two tests.
The first test is the condition of the while statement
while(std::cin>>value){...}
This condition tests the result of calling operator function operator >>
The second test is a condition within the operator. If the state of the stream std::cin
is good then the function tries to read an integer from the string. Otherwise it returns std::cin
with the current erroneous state of std::cin
.
In the while condition there is an expression
std::cin>>value
This expression must be evaluated. So this condition tests the result of the call of operator >>
.
The result of the operator is the stream std::cin
But it can be contextually converted to a bool value due to operator
explicit operator bool() const;
which returns the state of the stream
!fail().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With