This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

bad bug in map<>::insert


I have found what seems like a serious bug in the member
std::map<>::insert(iterator, const value_type&).  Passing a 
hint can cause the map to insert the value in the wrong place, 
silently violating the map's ordering invariant.

Do you agree this is a bug?

Nathan Myers
ncm at cantrip dot org

------------
$ cat mapbug.cc
#include <map>
#include <iostream>

int
main()
{
  std::map<int,int>  amap;
  amap[0] = 100;
  amap.insert(amap.begin(), std::make_pair(60, 100));

  for (std::map<int,int>::iterator it = amap.begin(); it != amap.end(); ++it)
    { std::cout << " (" << it->first << "," << it->second << ")"; }
  std::cout << std::endl;
  return 0;
}

$ g++ -v mapbug.cc
Reading specs from /usr/local/lib/gcc-lib/i686-pc-linux-gnu/3.0.1/specs
Configured with: ../gcc-3.0.1/configure --enable-languages=c,c++ --enable-c99 --enable-long-long --enable-threads=posix
Thread model: posix
gcc version 3.0.1

$ ./a.out  # should report "(0,100) (60,100)".
(60,100) (0,100)


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]