Here, the following expression returns cin, to which operator ! is applied:
std::cin >> x
The expression after ! must be placed within parentheses because of operator precedence rules:
Without the parentheses, operator ! would be evaluated first. In other words, the expression
!std::cin >> x
is equivalent to the expression
(!std::cin) >> x
This is probably not what is intended.
Although these operators are very convenient in Boolean expressions, one oddity has to be noted:
Double “negation” does not yield the original object:
• cin is a stream object of class istream.
• !!cin is a Boolean value describing the state of cin.
As with other features of C++, it can be argued whether the use of the conversions to a Boolean
value is good style. The use of member functions, such as fail(), normally yields a more readable
program:
std::cin >> x;
if (std::cin.fail()) {
...
}