std::cout
is an instance of std::ostream
. std::cout << "something"
calls one of the operator<<
overloads as would be done for any instance of std::ostream
.
It's "special" in that it references the console, but otherwise it behaves exactly as an ofstream
or an ostringstream
would.
EDIT: Chaining works just like it works for any other operators. Consider:
class MyType
{
friend std::ostream& operator<<(std::ostream& target, const MyType& source);
int val;
public:
MyType()
: val(0)
{ }
MyType& Add(int toAdd)
{
val += toAdd;
return *this;
}
};
MyType& operator+(MyType& target, int toAdd)
{
return target.Add(toAdd);
}
std::ostream& operator<<(std::ostream& target, const MyType& source)
{
target << source.val;
return target; //Make chaining work
}
int main()
{
MyType value1;
value1 + 2 + 3 + 4;
std::cout << value1 << " and done!" << std::endl;
}
In this case, the chaining for +s on MyType
work for the same reason the <<
s work on top of std::ostream
. Both +
and <<
are left-associative, which means they're evaluated from left to right. In the case of overloaded operators, the operator is replaced with the equivalent function call.
EDIT2: In a bit more detail:
Let's say you're the compiler and you're parsing
std::cout << value1 << " and done!" << std::endl;
First, <<
is left associative, so you start on the left. You evaluate the first <<
and turn it into the function call:
operator<<(std::cout, value1) << " and done!" << std::endl;
You then see that you once again have a std::ostream
(the result of the call to operator<<
), and a char *
, which you once again turn into the function call:
operator<<(operator<<(std::cout, value1)," and done!") << std::endl;
and so on until you've processed the entire statement.
cout <<
andcin >>
expressions are the reasons why C++ is so unintuitive and hard to learn. – Readiostream
library is complex, but there are plenty of awesome aspects to the language which aren't related to iostream at all (i.e. the STL). (If you don't like iostream, then don't use it. Use the bits in<cstdio>
or bits specific to your platform.) – Hazya << b;
should not have been an allowed statement; it's just like saying2 + 3;
, which -- even though valid -- makes no sense. If anything, they should've made it bea <<= b;
so that it would at least give the hint to the reader that some kind of assignment is actually taking place. – Read2 << 3
doesn't invoke operator overloading at all. As for making it illegal, you can write nonsense in any language. The language's job is not to prevent you from doing stupid things. – HazyTwo plus three
makes no sense as a sentence in English, the code2 + 3;
ora << b;
doesn't make really make much sense as a statement either, and the fact that it's actually valid makes C++ really hard to learn (not to mention making it easier for you to shoot yourself in the foot). – Read2 + 3;
, and it just leads to the confusion of both the programmer and the reader. Had they actually forced the use of some assignment operator likecout <<= "hi";
instead ofcout << "hi";
, IMHO the language (and consequently the I/O library) would've been a lot better. – Reada.Select((x) => x)
(wherea
is anIEnumerable
) in C# either, but the language lets you do it. If you want to be dumb you can be dumb anywhere. If you want "intuitive to a beginner" as a language requirement, then don't use C++. – HazySelect
is not part of the C# language. – Reada.Select((x) => x)
in C# as it is to writea << b;
in C++, because neither really makes sense as a statement. Not sure what you're trying to say, though... it just supports my point all the more. :) – ReadFILE *
point to a string. – HazyI can make feature x do nonsense!
; I'm saying Feature x is nonsense!, wherex
is the ability to statements likea << b;
. I'm not opposing operator overloading, nor am I saying anything about the I/O library. What I'm saying is,a << b;
should be syntactically invalid as a statement for the same reason thatC++ needs.
is an invalid sentence in English: neither makes much sense. Had it beena <<= b;
, it would've actually made sense, because=
implies the side effect of an assignment, whereas<<
doesn't. – Reada << b << c
? – Hazya << b << c
should be valid, buta << b << c;
shouldn't. (Notice the;
.) – Reada << b << c;
then. It's of the formexpression;
, which you can't remove because it's why (for example)a++;
works. – Hazya << b
needs to be a possible expression. I think we can both agree thatexpression;
can't go, as I've already illustrated. How exactly would you resolve that issue? – Hazya << b
needs to be a possible expresssion. No, we do not both agree that any expression should be a valid statement; particularly,a << b;
should not be a valid statement, because the<<
operator does not convey the fact that it is causing an assignment. And by the way, C# does not allow that. – Read1 << 2;
, or anything of the forma << b
, for that matter:error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement
– ReadC++ is not very.
be a grammatically correct sentence? – Reada + b;
in C++ is likeA plus B.
in English. The latter doesn't make sense as a sentence, and so neither should the former as a statement... I think it follows very logically, given that humans are meant to be reading code and not computers. – Reada << b
isn't necessarily causing an assignment. When it comes to streams, the newbie you're so keen to protect should be thinking of this is "stream b through a", so the vaguely arrow-like and directional nature of the less-than character is helpful, and forcout
they don't care about buffering: they just know the data's sent and gone, so assignment would be misleading ("hey, doesn't it get stuck in cout? can't I get it back?"). And other languages do adopt C++ style: e.g. Ruby:$stdout << 99 << " red balloons"
... look familiar? – Needlefishx + y
should not changex
. (Ditto with<<
.) – ReadGiven the hands-on teaching experience Stroustrup has had I'm sure he'd have done something about << if new developers had trouble with it.
So you think beginners have no trouble with it? For one thing, I had trouble with it when I was a beginner. For another thing, I'm not really alone; my friends ask me the same question when they're learning C++, and even others ask this same question on SO. I doubt that Stroustrup had the eye of a beginner when he designed C++, and I'm failing to see why you called this a cheapshot. – Read