Reputation: 28316
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.
Upvotes: 3
Views: 146
Reputation: 3556
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.
Upvotes: 0
Reputation: 120731
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 istream
s) 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
?
Upvotes: 0
Reputation: 40877
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.
Upvotes: 3
Reputation: 477338
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"
Upvotes: 9
Reputation: 308391
All that is necessary is to look at the documentation for ifstream
and see that only operator>>
is defined.
Upvotes: -1
Reputation:
Unless var
defines the <<
operator or it is defined as a free function, the former is not valid.
Upvotes: 2