0

I always thought of cout and cin calls as something like this:

cout << someVar

tells the compiler we're dumping the value of someVar into cout, just as we could dump water into a physical stream. Likewise,

cin >> someVar

tells the compiler to fill someVar with data from cin (everything before whitespace), just as we might fill a bucket with water from a physical stream.

But << and >> are bitwise operators. << shifts the bits to the left, while >> shifts the bits to the right. Why doesn't, say, cout << Hello World and cout << My name is John make the console not say "Hello World"?

Edit: This is not a duplicate of "Operator overloading" because while that question may implicitly address the << and >> operators, my question as presently constituted does not address overloading.

  • 3
    C++ allows *overloading* of operators. – Kerrek SB Feb 25 '16 at 19:34
  • 1
    because c++ allows operator overloading. you can change what an operator means/does at will, basically. – Marc B Feb 25 '16 at 19:34
  • Voting to reopen on the grounds that, although the linked question has an answer related to the bit wise operators, the question itself deals with the rules and idioms of operator overloading. –  Jun 11 '16 at 09:05
  • While the linked question may implicitly address the << and >> operators, my question as presently constituted does not address overloading. –  Jun 11 '16 at 09:14
  • Guys, I'm not satisfied that this question is a duplicate. The linked duplicate appears to deal with the rules & idioms of operator overloading - in other words, how to overload an operator. My question asks why C++ has you use the bit shift operators to insert output into a stream and extract output from a stream. –  Nov 03 '21 at 21:19

6 Answers6

5

In C++ you can overload operators. Here >> and << are such overloaded operators, in this case members of std::basic_ostream (std::basic_istream), of the form

std::basic_ostream& operator<<(int); // overload for int

So when you write

std::cout << 42;

the call is translated to

std::cout.operator<<(42);

Note that operator<< and operator>> can also be defined as non-member functions, in which case they have a form similar to:

std::basic_ostream& operator<<(std::basic_ostream& os, const Foo& foo)

where Foo is some custom type. This is how you implement in general stream extraction and insertion, overloading the non-member operator<< or operator>>, since you don't have access to the internals of std::basic_ostream or std::basic_istream.

See e.g. this for more details.

vsoftco
  • 55,410
  • 12
  • 139
  • 252
3

Remember that in C++ they're not literally bit shift operators, they're just operators. You can define them to mean anything you want, and in the context of a stream they perform input and output operations.

Don't get too hung up on "tells the compiler" stuff. That doesn't really mean anything. Instead think of "is defined to mean" in the code. There's nothing magical about the Standard Library, it's all just C++ code like you could write yourself. The compiler is just responsible for crunching that down to machine code.

You could write your own << operator on a class you've defined that adds things to an array, for example, or make a special version for the output streams that handles things a particular way. It's entirely up to you.

tadman
  • 208,517
  • 23
  • 234
  • 262
  • So, when used for streams, they're not bitwise operators. –  Feb 25 '16 at 19:37
  • 1
    They're redefined to mean something else entirely. C++ has a number of pre-defined operators, all of which can be redefined to mean just about anything. On anything but fundamental types like `int` or `char` you will need to look up what they do. For example, [`+` on strings](http://en.cppreference.com/w/cpp/string/basic_string/operator%2B) combines them together. If you ever see a method called `operator` something, that's what it's doing. – tadman Feb 25 '16 at 19:57
1

The << is an operator. And in C++ you can overload operators to do different things on different types.

The compiler is able to determine the type of that is being operated on and execute code differently. So for streams it sends data where it needs to go and is handled appropriately.

Barry
  • 286,269
  • 29
  • 621
  • 977
sedavidw
  • 11,116
  • 13
  • 61
  • 95
1

For the default uses of << and >>:

C++ : Whats does this sign “<<” do?

<< and >> can also be overloaded:

http://en.cppreference.com/w/cpp/language/operators

Edit This question may be a duplicate of this one:

How does the << Operator exactly work?

Community
  • 1
  • 1
star2wars3
  • 83
  • 1
  • 10
1

So << and >> are bitwise operators in the right context, but they become stream operators in a different context. Just like you can overload a function to accept different parameters and behave differently given those different parameters, these operators can (and have) been overridden in particular contexts.

Stream insertion means putting something into a string (like cout << "Hello";), and in this situation, the left operand is a stream and the right operand, a string, knows what to do when we tell it to "dump" into the stream, as you said.

Stream extraction (>>) is another context where a typically bitwise operator takes on a completely different meaning. If the left operand supports this extraction (stream), and the value extracted matches the type of the right operand, then we're performing a stream operation rather than a bit shift.

phrz
  • 117
  • 2
  • 10
1

<< and >> are operators in C++. On primitive types they do bitwise shift, but they can be overloaded as member functions or to take any two arguments. Essentially, it's just a simpler way to call an overloaded T operator<< (...) function.

Below are the library overloads provided for std::istream and std::ostream.

http://www.cplusplus.com/reference/ostream/ostream/operator%3C%3C/ http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/

You can overloaded to take user defined types as arguments.

http://www.tutorialspoint.com/cplusplus/cpp_overloading.htm

Ramon
  • 1,169
  • 11
  • 25