Bug 8529 - wontcompile copy() of map to ostream_iterator
Summary: wontcompile copy() of map to ostream_iterator
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 3.2
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2002-11-11 09:36 UTC by j.tevessen
Modified: 2016-02-04 02:59 UTC (History)
1 user (show)

See Also:
Host: i686-pc-linux-gnu
Target: i686-pc-linux-gnu
Build: i686-pc-linux-gnu
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description j.tevessen 2002-11-11 09:36:02 UTC
	g++ doesn't seem to find the matching operator<< when
	std::copy()ing a std::map to an output_iterator
	Error is like this:

/usr/include/c++/3.2/bits/stream_iterator.h: In member function 
   td::ostream_iterator<_Tp, _CharT, _Traits>& std::ostream_iterator<_Tp, 
   _CharT, _Traits>::operator=(const _Tp&) [with _Tp =
std::pair<std::string, 
   int>, _CharT = char, _Traits = std::char_traits<char>]':
mapproblem.cc:228:   instantiated from OutputIter std::__copy(_InputIter,
_InputIter, _OutputIter, std::input_iterator_tag) [with _InputIter =
std::_Rb_tree_iterator<std::pair<const std::string, int>, std::pair<const
std::string, int>&, std::pair<const std::string, int>*>, _OutputIter =
std::ostream_iterator<std::pair<std::string, int>, char,
std::char_traits<char> >]'
mapproblem.cc:260:   instantiated from OutputIter
std::__copy_aux2(_InputIter, _InputIter, _OutputIter, __false_type) [with
_InputIter = std::_Rb_tree_iterator<std::pair<const std::string, int>,
std::pair<const std::string, int>&, std::pair<const std::string, int>*>,
_OutputIter = std::ostream_iterator<std::pair<std::string, int>, char,
std::char_traits<char> >]'
mapproblem.cc:298:   instantiated from OutputIter
std::__copy_ni2(_InputIter, _InputIter, _OutputIter, __false_type) [with
_InputIter = std::_Rb_tree_iterator<std::pair<const std::string, int>,
std::pair<const std::string, int>&, std::pair<const std::string, int>*>,
_OutputIter = std::ostream_iterator<std::pair<std::string, int>, char,
std::char_traits<char> >]'
mapproblem.cc:321:   instantiated from OutputIter
std::__copy_ni1(_InputIter, _InputIter, _OutputIter, __false_type) [with
_InputIter = std::_Rb_tree_iterator<std::pair<const std::string, int>,
std::pair<const std::string, int>&, std::pair<const std::string, int>*>,
_OutputIter = std::ostream_iterator<std::pair<std::string, int>, char,
std::char_traits<char> >]'
mapproblem.cc:342:   instantiated from OutputIter std::copy(_InputIter,
_InputIter, _OutputIter) [with _InputIter =
std::_Rb_tree_iterator<std::pair<const std::string, int>, std::pair<const
std::string, int>&, std::pair<const std::string, int>*>, _OutputIter =
std::ostream_iterator<std::pair<std::string, int>, char,
std::char_traits<char> >]'
mapproblem.cc:21:   instantiated from here
/usr/include/c++/3.2/bits/stream_iterator.h:141: no match for 
std::basic_ostream<char, std::char_traits<char> >& << const 
   std::pair<std::string, int>&' operator
/usr/include/c++/3.2/bits/ostream.tcc:55: candidates are: 
   std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, 
   _Traits>::operator<<(std::basic_ostream<_CharT, 
   _Traits>&(*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, 
   _Traits = std::char_traits<char>]
(...)

Release:
3.2

Environment:
System: Linux aris.dummy.de 2.4.19-pre8 #1 Sam Mai 4 18:10:42 CEST 2002 i686 unknown
Architecture: i686

	
host: i686-pc-linux-gnu
build: i686-pc-linux-gnu
target: i686-pc-linux-gnu
configured with: ../configure --prefix=/usr --enable-languages=c,c++

How-To-Repeat:
	Try to compile this:
#include <cstdlib>
#include <iostream>
#include <string>
#include <map>
#include <algorithm>
#include <iterator>

std::ostream&
operator<< (std::ostream& ostr, const std::pair<std::string,int>& mypair) {
  return ostr << mypair.first;
}

int
main (void) {
  std::map<std::string,int> mymap;

  mymap[std::string("myid1")] = 1;
  mymap[std::string("myid2")] = 2;

  std::copy (mymap.begin (), mymap.end (),
             std::ostream_iterator<std::pair<std::string,int> >
             (std::cout, " "));

  return EXIT_SUCCESS;
}
Comment 1 j.tevessen 2002-11-11 09:36:02 UTC
Fix:
	Really don't know. However, I had no problem compiling this
	with Intel's C++ 5.0 compiler on Linux
Comment 2 Wolfgang Bangerth 2002-11-11 09:50:53 UTC
State-Changed-From-To: open->closed
State-Changed-Why: Your op<< is in the global namespace, but at the place where
    it is used (inside std::copy and the ostream_iterator), only
    operators in namespace std are looked up because both operands
    are in namespace std. There are several other instances
    of this bug/feature in the database.
    
    You can work around the problem by putting the op<< into
    namespace std.
Comment 3 Annaliese Saint 2016-02-04 02:59:41 UTC Comment hidden (spam)