[Bug c++/43014] New: map [] behaviour is inconsistent

2010-02-09 Thread gcc_bugzilla dot 20 dot marcelitom at inboxclean dot com
My first bug report, please let me know if I am missing anything.

Running...

$ cat minimal.cpp && echo "-" && g++ -Wall minimal.cpp && a.out

Produces...

#include 
#include 
#include 
using namespace std;

int main() {
  map hash;
  string sElement;

  sElement = "ab1";
  cout << sElement.c_str() << (( hash.find(sElement.c_str()) != hash.end() )
 ? "\t\tfound\n"
 : "\t\tnot found\n");

  sElement = "ab2";
  cout << sElement.c_str() << (( hash.find(sElement.c_str()) != hash.end() )
 ? "\t\tfound\n"
 : "\t\tnot found\n");


//  string aux = sElement;
  if( hash.find(sElement.c_str()) == hash.end() )
hash[sElement.c_str()] = 7;

  sElement = "ab3";
  cout << sElement.c_str() << (( hash.find(sElement.c_str()) != hash.end() )
 ? "\t\tfound\n"
 : "\t\tnot found\n");

  return 0;
}
-
ab1 not found
ab2 not found
ab3 found

There, the last line should be "ab3 not found", which can be
obtained by removing "//" from the only commented line.

$ cat /proc/version
Linux version 2.6.31-19-generic (bui...@palmer) (gcc version 4.4.1 (Ubuntu
4.4.1-4ubuntu8) ) #56-Ubuntu SMP Thu Jan 28 01:26:53 UTC 2010

$ g++ -v
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.4.1-4ubuntu9'
--with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs
--enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared
--enable-multiarch --enable-linker-build-id --with-system-zlib
--libexecdir=/usr/lib --without-included-gettext --enable-threads=posix
--with-gxx-include-dir=/usr/include/c++/4.4 --program-suffix=-4.4 --enable-nls
--enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc
--enable-targets=all --disable-werror --with-arch-32=i486 --with-tune=generic
--enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu
--target=i486-linux-gnu
Thread model: posix
gcc version 4.4.1 (Ubuntu 4.4.1-4ubuntu9)


-- 
   Summary: map [] behaviour is inconsistent
   Product: gcc
   Version: unknown
            Status: UNCONFIRMED
      Severity: normal
  Priority: P3
 Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: gcc_bugzilla dot 20 dot marcelitom at inboxclean dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43014



[Bug libstdc++/43014] map [] behaviour is inconsistent

2010-02-09 Thread gcc_bugzilla dot 20 dot marcelitom at inboxclean dot com


--- Comment #5 from gcc_bugzilla dot 20 dot marcelitom at inboxclean dot 
com  2010-02-10 04:00 ---
Thanks Andrew and Paolo, apologies for my mistake. I have learnt something.

According to what you wrote, a "seemingly natural" code like

  for(;;) {
sElement = SomeString(...);

if( hash.find(sElement.c_str()) == hash.end() )  //not found
  hash[sElement.c_str()] = SomeNumber(...);
  }

is wrong, which comes to me as a surprise. It probably means I need to learn
more.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43014



[Bug libstdc++/43014] map [] behaviour is inconsistent

2010-02-10 Thread gcc_bugzilla dot 20 dot marcelitom at inboxclean dot com


--- Comment #8 from gcc_bugzilla dot 20 dot marcelitom at inboxclean dot 
com  2010-02-10 21:23 ---
Thanks Paolo. I did understand that map was passing a . I wrongly
assumed that it was using it to copy the pointed string to the map, but I
learned from your first post that it was only copying the pointer.

May be I did not express myself clear, my surprise was that it was only copying
the pointer and not the string.

And yes, I wanted to use a string in the first place... I don't know where I
got that it was not possible to use string as a template for map, I was wrong.
I will use string then, thanks!


-- 

gcc_bugzilla dot 20 dot marcelitom at inboxclean dot com changed:

   What|Removed |Added

 CC||gcc_bugzilla dot 20 dot
   ||marcelitom at inboxclean dot
   |    |com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43014