Find the answer to your Linux question:
Results 1 to 3 of 3
I was playing around with STL's ostream_iterator and tried to use it to output non-trivial types by overloading the stream insertion operator. I kept getting template errors in the iterator's ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie
    Join Date
    Nov 2004
    Location
    New York
    Posts
    150

    C++: Overloading << for streams; namespace std


    I was playing around with STL's ostream_iterator and tried to use it to output non-trivial types by overloading the stream insertion operator. I kept getting template errors in the iterator's system header claiming that it couldn't resolve the operator call, even though I defined it in my main.cpp before I included the header. Eventually I tried surrounding the definition with namespace std {} and that fixed it.

    What I don't understand is why the header could not see my function when it was outside of std and in the global scope. The only reason I can think of is that another standard overloaded operator definition inside std took precedence, even though it didn't fit well at all.

    Strangely enough, when I defined my operator within std, I could place the definition at any point I liked in my file, even after my main function without a prototype.

    Code:
    namespace std &#123; // would not work when declared in global scope
    ostream& operator<< &#40;ostream& os, const pair<int, int>& arg&#41;
    &#123;
    	os << "&#40;" << arg.first << "," << arg.second << "&#41;";
    &#125;
    &#125;
    ...
    ostream_iterator< pair<int, int> > oi&#40;cout, " "&#41;; // construct ostream_iterator to write to cout when assigned a pair of ints
    *oi = pair<int, int>&#40;1, 2&#41;;
    \"Nifty News Fifty: When news breaks, we give you the pieces.\" - Sluggy Freelance

  2. #2
    Super Moderator Roxoff's Avatar
    Join Date
    Aug 2005
    Location
    Nottingham, England
    Posts
    3,812
    I think the reason is that the compiler knows what namespace it's supposed to be using, and hence looks inside that first for a matching method. Even though it's a poor match, it still comes out as a match, and so gets used in preference to anything in the global scope.
    Linux user #126863 - see http://linuxcounter.net/

  3. #3
    Linux Newbie
    Join Date
    Nov 2004
    Location
    New York
    Posts
    150
    Code:
    std&#58;&#58;ostream& operator+ &#40;std&#58;&#58;ostream& os, const std&#58;&#58;pair<int, int>& arg&#41;
    &#123;
    	os << "&#40;" << arg.first << "," << arg.second << "&#41;";
    &#125;
    
    namespace aaa &#123;
    std&#58;&#58;ostream& operator+ &#40;std&#58;&#58;ostream& os, int i&#41; &#123;return os;&#125; // dummy overload
    void test &#40;&#41;
    &#123;
    	std&#58;&#58;cout + std&#58;&#58;pair<int, int>&#40;1, 2&#41;;
    &#125;&#125;
    The dummy overload confuses the compiler into not realizing there is a choice outside of the namespaces aaa and std. If I move the dummy to global scope, there's no problem.

    Guess I'm going to be looking up the overload resolution rules. Sigh... it seems kind of inelegant that to use some of the features of the standard library, you have to invade its namespace.
    \"Nifty News Fifty: When news breaks, we give you the pieces.\" - Sluggy Freelance

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •