if (cin >> x) - Why can you use that condition?
I have been using "Accelerated C++" to learn C++ over the summer, and there's a concept which I don't seem to understand properly.
Why is
int x;
if (cin >> x){}
equivalent to
cin >> x;
if (cin){}
By looking at the code, it seems to me that we're using cin as a variable. But, I thought it was a function. Why can we use cin in this way when it is x that has whatever value we input into our keyboard?
cin
is an object of class istream
that represents the standard input stream. It corresponds to the cstdio
stream stdin
. The operator >>
overload for streams return a reference to the same stream. The stream itself can be evaluated in a boolean condition to true or false through a conversion operator.
cin
provides formatted stream extraction. The operation
cin >> x;
where "x" is an int will fail if a non-numeric value is entered. So:
if(cin>>x)
will return false
if you enter a letter rather than a digit.
This website on tips and tricks using C++ I/O will help you too.
Note: Answer updated four years after the fact to address both C++98/03 and C++11 (and beyond).
std::cin
is an instance of a std::istream
. That class provides two overloads that pertain to this question.
-
operator >>
reads data from the stream into the target variable if that is possible. If the immediate contents of the stream cannot be translated into the type of the target variable, the stream is instead marked as invalid and the target variable is left untouched. Regardless of the success/failure of the operation, the return value is a reference to the stream. - Either
operator void*()
(pre-C++11), which converts the stream reference to avoid*
pointer, orexplicit operator bool()
(C++11), which converts the stream reference to a boolean. The result of this conversion is a non-null pointer (pre-C++11) ortrue
(C++11) if the stream is valid, but the null pointer (pre-C++11) orfalse
(C++11) if the stream isn't valid.
An if
statement needs either a boolean, an integer, or a pointer as the quantity to be tested. The result of std::cin >> x
is a reference to an istream
, which is none of the above. However, the class istream
does have those conversion operators which can be used to transform the istream
reference to something usable in an if
statement. It is the version-specific conversion operator that the language uses for the if
test. Since failure to read marks the stream as invalid, the if
test will fail if the read didn't work.
The reason for the more convoluted operator void*
conversion member prior to C++11 is that it wasn't until C++11 that the already existing explicit
keyword was extended to apply to conversion operators as well as constructors. A non-explicit operator bool()
would have presented far too many opportunities for programmers to shoot themselves in the foot. There are problems with operator void*()
as well. The "safe bool idiom" would have been a fix, but simply extending explicit
accomplished exactly what the safe bool idiom accomplishes, and without having to use a lot of SFINAE magic.
cin
is a (global) variable of type istream
, not a function.
The istream
class overrides the >>
operator to perform input and return a reference to the object you called it on (cin
).
cin
is variable in std
namespace.
operator>>
return reference to cin
, because of it you can write: cin >> a >> b
, instead of cin >> a; cin >> b;