public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* STL question
@ 1998-09-24  3:26 Alexander Samoilov
  1998-09-26  0:52 ` Alexandre Oliva
  0 siblings, 1 reply; 7+ messages in thread
From: Alexander Samoilov @ 1998-09-24  3:26 UTC (permalink / raw)
  To: egcs

STL experts!

Could you help me?

The following code compiles ok:
----------------------------------------------
#include <set.h>
#include <map.h>
#include <multimap.h>
#include <list.h>
#include <hash_set.h>
#include <hash_map.h>


struct ltstr {
    bool operator() (char* const s1, char* const s2) const {
	return strcmp(s1, s2) < 0;
    }
};

typedef map<char* const, int, ltstr> hmap;

inline bool cmp(pair<char* const,int> p1, pair<char* const,int> p2)
{
    return strcmp(p1.first, p2.first) != 0;
}


int main()
{
    hmap ins1, ins2, ins_diff;

    insert_iterator<hmap> ii1(ins_diff,ins_diff.begin());

    set_difference(ins1.begin(), ins1.end(),
	           ins2.begin(), ins2.end(),
		   ii1, cmp);
}
----------------------------------------------------

But if we substitute map for hash_map, the compiler refuses
to compile point-blank:

----------------------------------------------------

#include <set.h>
#include <map.h>
#include <multimap.h>
#include <list.h>
#include <hash_set.h>
#include <hash_map.h>


struct eqstr {
    bool operator() (char* const s1, char* const s2) const {
	return strcmp(s1, s2) == 0;
    }
};

typedef hash_map<char* const, int, hash<char*>, eqstr> hmap;

inline bool cmp(pair<char* const,int> p1, pair<char* const,int> p2)
{
    return strcmp(p1.first, p2.first) != 0;
}


int main()
{
    hmap ins1, ins2, ins_diff;

    insert_iterator<hmap> ii1(ins_diff,ins_diff.begin());

    set_difference(ins1.begin(), ins1.end(),
	           ins2.begin(), ins2.end(),
		   ii1, cmp);
}
--------------------------------------------------------------

with the following messages:

--------------------------------------------------------------
home/spike/egcs-gas-bin/include/g++/stl_iterator.h: In method `class insert_iterator<hash_map<char *const,int,hash<char *>,eqstr,__default_alloc_template<false,0> > > & insert_iterator<hash_map<char *const,int,hash<char *>,eqstr,__default_alloc_template<f
alse,0> > >::operator =<hmap>(const struct pair<char *const,int> &)':
/home/spike/egcs-gas-bin/include/g++/stl_algo.h:2213:   instantiated from `set_difference<__hashtable_iterator<pair<char *const,int>,char *const,hash<char *>,select1st<pair<char *const,int> >,eqstr,__default_alloc_template<false,0> >, __hashtable_iterator
<pair<char *const,int>,char *const,hash<char *>,select1st<pair<char *const,int> >,eqstr,__default_alloc_template<false,0> >, insert_iterator<hash_map<char *const,int,hash<char *>,eqstr,__default_alloc_template<false,0> > >, bool (*)(pair<char *const,int>,
 pair<char *const,int>)>(__hashtable_iterator<pair<char *const,int>,char *const,hash<char *>,select1st<pair<char *const,int> >,eqstr,__default_alloc_template<false,0> >, __hashtable_iterator<pair<char *const,int>,char *const,hash<char *>,select1st<pair<ch
ar *const,int> >,eqstr,__default_alloc_template<false,0> >, __hashtable_iterator<pair<char *const,int>,char *const,hash<char *>,select1st<pair<char *const,int> >,eqstr,__default_alloc_template<false,0> >, __hashtable_iterator<pair<char *const,int>,char *c
onst,hash<char *>,select1st<pair<char *const,int> >,eqstr,__default_alloc_template<false,0> >, insert_iterator<hash_map<char *const,int,hash<char *>,eqstr,__default_alloc_template<false,0> > >, bool (*)(pair<char *const,int>, pair<char *const,int>))'
hm1.cc:32:   instantiated from here
/home/spike/egcs-gas-bin/include/g++/stl_iterator.h:398: no matching function for call to `hash_map<char *const,int,hash<char *>,eqstr,__default_alloc_template<false,0> >::insert (__hashtable_iterator<pair<char *const,int>,char *const,hash<char *>,select1
st<pair<char *const,int> >,eqstr,__default_alloc_template<false,0> > &, const pair<char *const,int> &)'
/home/spike/egcs-gas-bin/include/g++/stl_hash_map.h:139: candidates are: hash_map<char *const,int,hash<char *>,eqstr,__default_alloc_template<false,0> >::insert<char *const, int, hash<char *>, eqstr, alloc>(const pair<char *const,int> &)
--------------------------------------------------------------

Could you give me some advice how to remedy the situation?

Best regards.

Alexander Samoilov


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: STL question
  1998-09-24  3:26 STL question Alexander Samoilov
@ 1998-09-26  0:52 ` Alexandre Oliva
  1998-09-28  9:26   ` Joe Buck
  0 siblings, 1 reply; 7+ messages in thread
From: Alexandre Oliva @ 1998-09-26  0:52 UTC (permalink / raw)
  To: Alexander Samoilov; +Cc: egcs

Alexander Samoilov <spike@Gambit.Msk.SU> writes:

> typedef hash_map<char* const, int, hash<char*>, eqstr> hmap;

> int main() { hmap ins_diff;
>     insert_iterator<hmap> ii1(ins_diff,ins_diff.begin());

Unlike map, hash_map does not support insert(pos,x), so insert
iterators do not work on it :-(

Instead, you may want to replace ii1 with:

      bind1st(mem_fun(insert_meth), &ins_diff)

   given:

      pair<hmap::iterator,bool> (hmap::*insert_meth)(const hmap::value_type&)
	= &hmap::insert;
      
Perhaps insert iterators should be specialized for hash maps...

-- 
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: STL question
  1998-09-26  0:52 ` Alexandre Oliva
@ 1998-09-28  9:26   ` Joe Buck
  0 siblings, 0 replies; 7+ messages in thread
From: Joe Buck @ 1998-09-28  9:26 UTC (permalink / raw)
  To: Alexandre Oliva; +Cc: spike, egcs

Alexandre Oliva writes:

> Unlike map, hash_map does not support insert(pos,x), so insert
> iterators do not work on it :-(

I think that this is a design bug.  STL containers should be as orthogonal
as possible.  (and since hash_map is an extension, it can be fixed without
having to worry about violating the standard.

> Perhaps insert iterators should be specialized for hash maps...

Or perhaps insert(iterator, value) should be implemented for hash maps,
in an analogous way as for maps (the iterator position is only taken as
a hint, which can be ignored if necessary).

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: STL Question.
  1998-02-25 18:48 ` Joe Buck
@ 1998-02-25 19:37   ` Chris Dern
  1998-02-25 18:56     ` Joe Buck
  0 siblings, 1 reply; 7+ messages in thread
From: Chris Dern @ 1998-02-25 19:37 UTC (permalink / raw)
  To: Joe Buck; +Cc: egcs

The program dumps core, when run. If I use -frepo, it won't link correctly
after collect2 decideds to re-compile (-fpreo), the linker complains of
missing functions.

Joe Buck wrote:

> > Quick question.
> > I'm trying to nest a STL list in a map, like so.
> >
> > typedef list<Base*> BaseList;
> >
> > map<String,BaseList> itsMap;
> >
> >
> > However this won't compile correctly if I use the repo, and if I just
> > try and compile it, I get a core dump. Anyone else have problems with
> > this?
>
> What dumps core, the program or the compiler?
>
> While we should investigate the problem, there is no good reason to use
> -frepo on your platform.  You'll get pretty much the same executable
> without it (if your binutils is recent enough: 2.8.0.x or newer).




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: STL Question.
  1998-02-25 19:37   ` Chris Dern
@ 1998-02-25 18:56     ` Joe Buck
  0 siblings, 0 replies; 7+ messages in thread
From: Joe Buck @ 1998-02-25 18:56 UTC (permalink / raw)
  To: Chris Dern; +Cc: jbuck, egcs

> The program dumps core, when run. If I use -frepo, it won't link correctly
> after collect2 decideds to re-compile (-fpreo), the linker complains of
> missing functions.

I suspect that the core dump is a bug in your program: putting pointers
in STL objects can be a bit tricky, as you must do your own memory 
management.  As for the missing functions, this could be a bug, or
it could be that the functions are actually missing (template definitions
not visible).  One way to test that is to build without -frepo and
see if you are missing anything.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: STL Question.
  1998-02-25  9:33 STL Question Chris Dern
@ 1998-02-25 18:48 ` Joe Buck
  1998-02-25 19:37   ` Chris Dern
  0 siblings, 1 reply; 7+ messages in thread
From: Joe Buck @ 1998-02-25 18:48 UTC (permalink / raw)
  To: Chris Dern; +Cc: egcs

> Quick question.
> I'm trying to nest a STL list in a map, like so.
> 
> typedef list<Base*> BaseList;
> 
> map<String,BaseList> itsMap;
> 
> 
> However this won't compile correctly if I use the repo, and if I just
> try and compile it, I get a core dump. Anyone else have problems with
> this?

What dumps core, the program or the compiler?

While we should investigate the problem, there is no good reason to use
-frepo on your platform.  You'll get pretty much the same executable
without it (if your binutils is recent enough: 2.8.0.x or newer).

^ permalink raw reply	[flat|nested] 7+ messages in thread

* STL Question.
@ 1998-02-25  9:33 Chris Dern
  1998-02-25 18:48 ` Joe Buck
  0 siblings, 1 reply; 7+ messages in thread
From: Chris Dern @ 1998-02-25  9:33 UTC (permalink / raw)
  To: egcs

Quick question.
I'm trying to nest a STL list in a map, like so.

typedef list<Base*> BaseList;

map<String,BaseList> itsMap;


However this won't compile correctly if I use the repo, and if I just
try and compile it, I get a core dump. Anyone else have problems with
this?

This is on a RH 4.0 Egcs 1.01, i486

Chris




^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~1998-09-28  9:26 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-09-24  3:26 STL question Alexander Samoilov
1998-09-26  0:52 ` Alexandre Oliva
1998-09-28  9:26   ` Joe Buck
  -- strict thread matches above, loose matches on Subject: below --
1998-02-25  9:33 STL Question Chris Dern
1998-02-25 18:48 ` Joe Buck
1998-02-25 19:37   ` Chris Dern
1998-02-25 18:56     ` Joe Buck

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).