home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 32,126 of 33,346   
   Marc to James K. Lowden   
   Re: ostream_iterator for map   
   13 Apr 12 01:49:52   
   
   From: marc.glisse@gmail.com   
      
   "James K. Lowden"  wrote:   
      
   > I want to copy a std::map to std::cout.  I define operator<< for the   
   > map's value_type.  I can use that operator by defererencing a the map's   
   > iterator.  Why can't I compile std::copy with ostream_iterator?   
   >   
   > The program that follows compiles and runs as is.  Change it to #if 0   
   > instead, and get "no match for 'operator<<' ".  g++ says there are many   
   > candidates, all standard, none mine.   
   [...]   
   > #include    
   > #include    
   > #include   
   > using namespace std;   
   >   
   > typedef   map the_map;   
   >   
   > ostream& operator<<( ostream& os, const the_map::value_type& value )   
   > {   
   >   return os << "(" << value.first << ", " << value.second << ")";   
   > }   
   >   
   > int main( int argc, char *argv[] )   
   > {   
   >   the_map m;   
   >   m[1] = 1.0F;   
   >   
   > #if 1   
   >   cout << *m.begin() << endl;   
   > #else   
   >   copy( m.begin(), m.end(),   
   > 	ostream_iterator(cout, "\n") );   
   > #endif   
   >   return 0;   
   > }   
      
   If you put your operator<< in the std namespace, it works. If you   
   replace int or float with some class you declared in the global   
   namespace, it works too. Looks like argument-dependent lookup doesn't   
   see a reason to search there when all its types are from the std   
   namespace or builtins.   
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca