In C++, is `var << ifstream` the same as `ifstream >> var`?

c++, extract, ifstream, insertion

Solution

They're not the same. `foo << bar` is `foo.operator<<(bar)` or `operator<<(foo, bar)`, while `bar >> foo` is `bar.operator>>(foo)` or `operator>>(bar, foo)`.

They're just different things. Whether any of those versions exist, let alone whether if two versions exist they do the same thing, is entirely dependent on what's in your code.

For the standard iostreams, typically only the following two free functions, and no others, are defined for some user-defined type `T`:

std::ostream & operator<<(std::ostream &, T const &);  // for "os << x"
std::istream & operator>>(std::istream &, T &);        // for "is >> y"

Problem

Is `var << ifstream` the same as `ifstream >> var`? As far as I can tell, they should be exactly the same. But it's late and my brain is half-asleep, so I would like a clarification.

Original source