3

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.

Polynomial
  • 27,674
  • 12
  • 80
  • 107

6 Answers6

9

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"
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • 1
    Just goes to show that I shouldn't code when tired. Makes perfect sense now. Thanks :) – Polynomial May 06 '12 at 21:25
  • 4
    No problem. It's a good idea to keep in mind that operators in C++ can be overloaded all over the place; sometimes this crops up in very unexpected situations (e.g. there's a defect in the current standard concerning an erroneous use of the comma operator). But overloaded operators are just function calls, and there's no magic. – Kerrek SB May 06 '12 at 21:28
3

No. They call completely different functions. One calls operator >>, the other calls operator <<. Further the arguments are different for the two.

This is similar to asking if F(int,double) is the same as calling Q(double, int)--maybe, maybe not--though I can see why this isn't immediately apparent to someone new to the language. You first need to realize that overloaded operators are nothing special, they are just function calls.

Edward Strange
  • 40,307
  • 7
  • 73
  • 125
2

Unless var defines the << operator or it is defined as a free function, the former is not valid.

0

here is a little hint which shows up that both expressions are not the same. Operatoras can also be called via a very ugly syntax. See for yourself:

var << ifstream;

equals

var.operator<<(ifstream);

and

ifstream >> var;

equals

ifstream.operator>>(var);

EDIT: After beeing reminded by below comment - there is also a third option. operator>> may also be implemented as a function:

returntype operator>>(ifstream, var);
returntype operator>>(var, ifstream);

Since the signatures do not match they may be implemented differently.

matthias.

Matthias
  • 3,458
  • 4
  • 27
  • 46
0

No, it's not the same thing. The convention is that the stream argument is always to the left of the << >> operators and the values to be read/written to the right. And there is a good reason for this: these operators can be chained like

std::cout << "Hello" << ',' << " world";

(It works analogous for istreams) What happens here: the << operator is left-associative, so we have

((std::cout << "Hello") << ',') << " world";

where the types are

std::cout                         :   std::ostream
(std::cout << "Hello")            :   std::ostream
((std::cout << "Hello") << ',')   :   std::ostream

so at each time a operator<<(stream, value) is called, which gives the desired result. Now what you want is to turn the entire thing around, like

" world" >> ',' >> "Hello" >> std::cout;

which, at first sight, looks like it should do the same thing. But it doesn't, because this now resolves to

((" world" >> ',') >> "Hello") >> std::cout;

where you start of with an operator>>(const char*, char). Now how is this operator to know that the result will eventually be put in an std::ostream?

leftaroundabout
  • 117,950
  • 5
  • 174
  • 319
-1

All that is necessary is to look at the documentation for ifstream and see that only operator>> is defined.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • 1
    There is no reason why one could not define `operator <<` for a type that takes a stream as its second argument and reads from it. It's just not standard, expected methodology used by most people. – Edward Strange May 06 '12 at 21:20
  • @CrazyEddie, of course you can define anything you want. I took the question to mean do they both work out of the box, and the answer is **no**. – Mark Ransom May 06 '12 at 21:29
  • 1
    Unfortunately your answer also implies that one only need to look at the documentation for ifstream to know the answer, and this is quite misleading...to give benefit of doubt. – Edward Strange May 06 '12 at 21:34
  • 3
    It also implies that looking at an entry on cplusplus.com is equivalent to looking at the documentation. – Benjamin Lindley May 06 '12 at 21:41
  • @BenjaminLindley, if you know of better online documentation I'd love to hear it. – Mark Ransom May 06 '12 at 22:23
  • Sure: [This one](http://en.cppreference.com/w/Main_Page), though not complete yet, I've found it to be much more accurate. (props to StackOverflower [cubbi](http://stackoverflow.com/users/273767/cubbi) for the extensive work he's put into it). Neither source is definitive though. And I was really just taking issue with your wording which *seemed* to imply that cplusplus.com *was* in fact a definitive source. Specifically, it was your use of the word "the" before "documentation". Sorry for being a pedant, but IMO, that wording would leave the OP with the wrong impression. – Benjamin Lindley May 07 '12 at 00:05