Why << for cout?

Hi again,

I know it's a lame question but I'm a beginner. What is the significance of <<?

Example:
1
2
3
4
5
6
int main ()
{
  cout << "Hello World"; // Why use <<? Significance? Why not cout "Hello world";?
}



Cheers ^^
Basically << is actually an overloaded operator that can handle all kinds of different arguments. So cout can cope with different types of variables, not just string literals. It also means that it is type safe.

HTH
The use of arrows for redirection have been around since the early computers.

such as
1
2
cat < myfile
date > myfile


I'm not familiar with the development of C but assume they use the same concepts that unix, dos and other common OS's used to keep the commands similar.
Such as typing CTRL-C to cut CTRL-V to paste.
That probably calls the overload:
ostream& operator<< (ostream& os, const string& str);
We have an ostream (cout) and text on two sides of a binary operator, but we do have a return value too (cout).

The return value allows chaining operators:
std::cout << "Hello" << std::setw(42) << "world" << std:.endl;
Technically, one could have a keyword foo that picks up everything after it until ';' and shovels it into somewhere, but with the operator model cout is just one object, like any other ostream object, and we can dictate how our types do print by implementing overloaded operator.

Assuming there would be keyword "cout", what should happen here?
1
2
3
4
#include "Bar.h"
..
  Bar b;
  cout b;

How would author of class Bar define output formatting?
@SamuelAdams

OS stuff is rather different I think.

Unix was written in C (still is AFAIK), so things like your examples would have implemented with something similar to argument lists we have for main:

int main(int argc, char * argv[])

Even though there are often a large variety & combination or arguments, there is a finite list of combinations, so these are coded the same way as anyone would process argc and argv[] - it's just that there is a lot of code to do it.

The C++ << operator is rather different (as keskiverto is saying) because it can easily handle a variety of types including a user class. The combination of the args doesn't really matter any more because of the return value keskiverto is talking about. This would be a real pain in the ass in C programming, and C++ has the benefit of overloading your own << for your class.
I know it's a lame question but I'm a beginner. What is the significance of <<?

Ah, the weirdness of C++.

The "real" meaning of the << operator is that of shifting bits to the left.

For example:
suppose you have a value which is represented as:

10001111

10001111 << 3 == 01111000


In C++ however, operators can be overloaded... that is, redefined depending on the input data type.

And so operator<< was overloaded to provide formatted output in output streams (std::ostream family).
http://cplusplus.com/reference/ostream/ostream/operator%3C%3C/

Early critics of C++ used to make fun of the bitshift operators being recycled for streams.
They could have used a normal function instead of an operator but that would not be so nice if we chain many function calls.

1
2
// Using operator<<
std::cout << "Hello " << name << "!" << std::endl;

1
2
// Using a function called print
print(print(print(print(std::cout, "Hello "), name), "!"), std::endl);


The reason they picked operator<< is that it looks like arrows pointing to the left, which makes sense because the thing on the right is written/outputted to the thing on the left.
1
2
// Data flows from b to a.
a << b;

For input streams (e.g. std::cin) they use operator>> that instead points in the other direction.
1
2
// Data flows from a to b.
a >> b;
Last edited on
Or...

std::cout.print("Hello ").print(name).print("!").print(std::endl);
@Catfish3
That would not allow you to create your own overloads of print.
Last edited on
@ Peter87: I don't understand why not?
Because it's a member function. To add a member function you need to modify the class definition but you can't do that with the standard classes.
That slipped my mind, thanks.
I had a suspicion there was a good reason for you not doing what I did. ;)
Last edited on
> The reason they picked operator<< is that it looks like arrows pointing to the left,
> which makes sense because the thing on the right is written/outputted to the thing on the left.

The key decisions was to separate formatting from buffering, and to use the type-safe expression syntax (relying on operators << and >>). I made these decisions after discussions with my colleague Doug McIlroy at AT&T Bell Labs. I chose << and >> after experiments showed alternatives, such as < and >, comma, and = not to work well. - Stroustrup


We can think of many reasons why << and >> were eventually chosen:

a. As Peter87 pointed out, looks like arrows pointing to the left
b. Also, as pointed pout, symmetric with respect to input and output << and >>
c. Not very commonly used as a built-in; with stm < i 'less than' may be what comes to mind immediately.
d. Left-associative; so we can write stm << i << j ;
e. Has reasonably low precedence; so we can write stm << i + j ;
Regarding (c):
Lets assume that 'greater than' would have been selected as input operator.
1
2
3
4
istream foo;
Bar bar;
..
while ( foo > bar )

The input operator returns cin and cin converts to boolean true, if operation did not fail. The reader of the code (while cursing authors choice of variable-names and hunting for declarations) cannot help but wonder whether the code has input or comparison, since both can occur in condition. A bitshift is rare. A bitshift as conditional expression ...
Topic archived. No new replies allowed.