0

I don't have a good C++ book on hand and google pulls ups nothing useful.

How does this work? Conceptually what is going on here? Technically, is the prototype for operator<<() predefined, how did the writer of this know how to write it so that << is overloaded to output Container values?

Where can I go to look at the operator<<() so that I can overload it?

Also for an input you need a start and an end "place" c.begin(), c.end()...but for output you need one "place" ostream_iterator. This seems a bit asymmetrical.

template <typename Container> 
std::ostream& operator<<(std::ostream& os, const Container& c) 
{ 
    std::copy(c.begin(), c.end(),  
              std::ostream_iterator<typename Container::value_type>(os, " ")); 
    return os; 
}
sbi
  • 219,715
  • 46
  • 258
  • 445
  • You mean the prototypes allowed for the various operators? Have a look [here](http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B). – Matteo Italia Oct 29 '11 at 22:55
  • I had half a mind to vote to close this as a dupe of [the operator overloading FAQ entry](http://stackoverflow.com/questions/4421706/operator-overloading), but then I doubted, because it seems this asks some more than just how operator overloading works. I'm not sure. – sbi Oct 29 '11 at 22:56
  • It's always the same operator, only overloaded for different types, in particular, with `std::ostream &` (or another type of stream) as the left operand. In this case you must be careful to return a reference to the same stream that was passed to your operator to allow chaining. – Matteo Italia Oct 29 '11 at 22:59
  • Now we are talking...where is the right hand operand defined? –  Oct 29 '11 at 23:02
  • @ChrisAaker: With a map it's only going to work if you have `operator<<()` overloaded for a map's `value_type` (which is `std::pair`). – sbi Oct 29 '11 at 23:02
  • `param1` is not magical, it's just a reference to a stream... Correct for `param2`, the interesting thing here is that it's templated, so it actually would match *any* type, shadowing any other templated `operator<<` if it weren't for SFINAE. – Matteo Italia Oct 29 '11 at 23:05
  • Thanks for the good info. I need to learn terms like shadowing and what SFINAE is. –  Oct 29 '11 at 23:15
  • @sbi: Or `std::map::value_type` – Martin York Oct 29 '11 at 23:30
  • @ChrisAaker " In this context it is considered an insertion operator not a bit wise operator. May help with you google searching. – Joe McGrath Oct 29 '11 at 23:58

1 Answers1

1

This is pretty vague, but I'll give it a shot:

Technically, is the prototype for operator<<() predefined, how did the writer of this know how to write it so that << is overloaded to output Container values?

Where can I go to look at the operator<<() so that I can overload it?

You can overload operators for all user-defined types for which they are not already overloaded. See here for more info on this.

Also for an input you need a start and an end "place" c.begin(), c.end()...but for output you need one "place" ostream_iterator. This seems a bit asymmetrical.

Taking this as a question...
That's just how std::copy() is defined: It takes an input range (defined by begin and end iterators), and an output iterator for where to write. It assumes that, wherever it writes to, there's enough room. (If you take an output stream operator, there's always enough room.)

You might want to get yourself a good C++ book.

Community
  • 1
  • 1
sbi
  • 219,715
  • 46
  • 258
  • 445
  • For the second part, it's worth noting that requiring one to pass an end-of-range for the output would be completely redundant. It's pretty usual to go "copy A->B to the place starting C"; see: `memcpy`. – Lightness Races in Orbit Oct 29 '11 at 23:05