Q:

What does >> do when used with string stream?

I was reading the following code sample:
std::array<int, 4> parseMessage(const std::string& input) {
    std::stringstream ss(input);
    std::array<int, 4> message;
    int n;
    // Loop over all characters in the string and ignore the semicolons.
    for (int i = 0; ss >> n && i < 4; ++i) {
        message[i] = n;
        if (ss.peek() == ';') {
            ss.ignore();
        }
    }
    return message;
}

Can someone explain why someone would do ss>>n in the loop condition area ? It looks a bit odd to me.

A:

It will first read a an int from the stream and then evaluate if the stream is good. (because s >> n will return a reference to s).
Evaluating a stream as bool:

Returns true if the stream has no errors and is ready for I/O operations. Specifically, returns !fail().

So as soon as the stream fails to read an int or reaches the end of stream (e.g. end of input) it will evaluate as false and end the loop. 
So this code will extract up to 4 ints from a given input (4, because of the && i < 4).

