In C++ (and using the standard library facilities), if I want to read an integer from the standard input, I need to do this:
int x;
std::cin >> x;
And looking at the std::istream
page on cppreference, it seems there is no function such as std::cin.scan<int>()
which returns an int
prvalue; nor is there a freestanding function std::scan<int>(std::cin)
which does so.
My question is: Why?
It seems nonsensical to me to encourage people to:
- Define and name variables they may not need (e.g. as opposed to
func_taking_an_int(std::cin.scan<int>())
. - Split the definition and the initialization of a variable
So what's the rationale of only supporting the operator form?
Now, I know C++ istreams go back decades, but it's still weird to me (not to mention that an extra method/function could have been added later on).
Motivation: I saw this question and realized that, to our shame, we cannot offer the poster a better way to write their program other than using an uninitialized variable. I was assuming that, surely, we can help that poster bring the definition into the same statement as the initialization... but it seems we cannot.