Need a little clarification on the >> operator.

In my attempt to gain a less superficial understanding of it, I learned that the >> operator, which is usually presented to newbies with cin>>var; examples, reads right-to-left.

That is.

cin >> var;

in which cin is a specialized input stream and var; is the input being read (right-to-left, by the operator) into the cin stream.

Now, I know that in a given program's complex communication with the operating system (and by extension, the hardware) there exists a process which collects my input characters from whatever (keyboard, etc.) encapsulates it in a buffer, and gives the buffer to the istream to convert to objects of data types. Correct me if my understanding is wrong or (more than trivially) incomplete.

So.

Once my istream receives the characters or what-have-you and converts them to objects of types, is that when it gets stored in the var? Or does it get stored in the var before the istream receives the data object?

In the case of

1
2
cin >> a >> b >> c;
cout << a << b << c;


The cin stream collects the input from my keyboard and the cout stream sends it to my monitor.

If I typed 3 2 1

My output would be 3 2 1


My impression here is that the istream (somehow) stores the input into the variables and THEN reads the >> operator(s) which places the return value(s) of the operand variable(s) in the stream.

However, this case would still confuse me, because my impression is that >> reads right-to-left. So why, when I "stack" variables as with the statement

cin >> a >> b >> c;

does it read the values in the buffer into the variables in left-to-right order?

Does my misunderstanding lie with how istreams work, or with how the >> operator works?

EDIT: Or is there a two-fold standard whereby the typed values are read into the variables left-to-right, while the variables are read into the stream by the operator right-to-left?
Last edited on
No, your premise is wrong. The >> operator has always been left-to-right.

It is overloaded on streams because it looks pretty and is convenient. Given

cin >> a >> b >> c;

For convenience, we'll assume a, b, and c are of types A, B, and C.

First cin and a are passed to the operator:

istream& operator >> ( istream& ins, A& a )

which reads value into a or fails and sets the error condition on ins, then returns ins. This reduces the original expression:

1
2
3
cin >> a >> b >> c;
(cin >> a) >> b >> c;
cin >> b >> c;

Rinse and repeat, with operator >> ( istream&, B& ), then operator >> ( istream&, C& ).

When you are done, you are left with the original stream, which is useful in an if statement:

1
2
3
4
5
6
7
8
9
10
if (cin >> foo)
{
  yay();
}
else
{
  fooey();
  cin.clear();
  (resync cin here as appropriate)
}

Hope this helps.
It is amazing what trouble such a small misunderstanding has caused me.

Thank you.

I don't remember where I picked up that right-to-left nonsense...
Last edited on
When I was first learning C I missed a } somewhere, and spent a week and a half trying to figure out what my (ancient) compiler was trying to complain about. (Error messages were significantly less helpful than they are today, if that means anything.)

I didn't touch it again for years.
Topic archived. No new replies allowed.