Why does ostream_iterator not work as expected?
c++, stream
Solution
The problem is that the name lookup does not find your `operator<<(ostream& os, const PAIR& r)`. The code that tries to invoke the `operator<<` is in somewhere inside the `ostream_iterator<>` which is itself inside the `std` namespace. The name lookup looks around for the right function inside `ostream_iterator<>` and the `std` namespace; the argument dependent lookup does not help here because both of the parameters are in the `std` namespace, too.
So, my suggestion is (1) either to wrap your operator into `namespace std { }`, but that is UB, IIRC. Or (2) create a struct inheriting from `std::pair` to define a new type in your namespace, and using the ADL to find your `operator<<()`.
UPDATE:
My 3rd suggestion is to use a custom manipulator to print out the pair.
As for my 2nd suggestion, if you can use C++11, inheriting from `std::pair` should be easy (untested):
struct PAIR : std::pair
{
using std::pair::pair;
};
If you cannot use C++11, then I suggest using a custom manipulator.
Problem
Needless to say any more than the following code: ``` #include <utility> #include <vector> #include <iostream> #include <iterator> using namespace std; typedef pair<char, char> PAIR; ostream& operator <<(ostream& os, const PAIR& r) { return os << r.first; } int main() { vector<PAIR> coll; cout << coll[0]; // OK. // The following line will cause a compilation error! Why??? copy(coll.begin(), coll.end(), ostream_iterator<PAIR>(cout)); } ```