This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: C++ help, please? (fwd)
- To: hjl at lucon dot org (H.J. Lu)
- Subject: Re: C++ help, please? (fwd)
- From: Joe Buck <jbuck at Synopsys dot COM>
- Date: Tue, 16 Feb 99 15:26:19 PST
- Cc: egcs at egcs dot cygnus dot com
> I got this. Is this an egcs bug or is the code bogus?
In the future please ask people to use gnu.g++.help for these.
The code is extremely bogus, however egcs-1.1.1 gets messed up by it.
The current snapshot compiler gives much better diagnostics, so it
doesn't look like we have remaining bugs.
Bogosities in the code:
1. attempt to write to a const object: note
typedef const unsigned char ByteKeyID[8];
the user then writes
ByteKeyID kid;
and proceeds to assign to its elements.
2. wrong interface to 'insert' function. There is no insert(key,value)
interface. The user could write
hm[key] = value;
which is easy to write and to understand. insert takes a pair.
3. hash<T> specializations are only provided for certain types,
and arrays of 8 unsigned chars are not among them. The user must
supply a hash function.
4. The user appears to be counting on being able to pass arrays by value.
One way to do this safely is to wrap the array in a struct:
struct ByteKeyID {
unsigned char data[8];
};
but it simply won't work otherwise.
> ...
> Here's the code:
>
> #include <hash_map>
>
> typedef const unsigned char ByteKeyID[8];
>
> struct kcmp {
> bool operator() (ByteKeyID a, ByteKeyID b) const {
> return (memcmp(a, b, 8) == 0);
> }
> };
>
> typedef hash_map<ByteKeyID,
> int,
> hash<ByteKeyID>,
> kcmp>
> hmap;
>
> int main(int argc, char **argv)
> {
> int i=0;
> ByteKeyID kid;
> hmap hm;
>
> for (i = 0; i < 8; i++) kid[i] = 8-i;
> hm.insert(kid, 2);
>
> for (i = 0; i < 8; i++) kid[i] = i;
> hm.insert(kid, 10);
>
> for (i = 0; i < 8; i++) kid[i] = 8-i;
>
> cout << "Extracted first value: ";
> cout << hm[kid] << endl;
> }
>
> Here's what the compiler says:
>
> uri@alisan:/usr2/src/crypto-3.0 > g++ -I. -g -o v2-t v2-t.cc
> /usr/include/g++-2/stl_hash_map.h: In instantiation of `hash_map<const unsigned char[8],int,hash<const unsigned char[8]>,kcmp,__default_alloc_template<false,0> >::hash_map<unsigned char[8], int, hash<const unsigned char[8]>, kcmp, alloc>()':
> v2-t.cc:21: instantiated from here
> /usr/include/g++-2/stl_hash_map.h:78: new declaration `hash_map<unsigned char[8],int,hash<const unsigned char[8]>,kcmp,__default_alloc_template<false,0> >::hash_map<>()'
> /usr/include/g++-2/stl_hash_map.h:78: ambiguates old declaration `hash_map<const unsigned char[8],int,hash<const unsigned char[8]>,kcmp,__default_alloc_template<false,0> >::hash_map<unsigned char[8], int, hash<const unsigned char[8]>, kcmp, alloc>()'
> /usr/include/g++-2/stl_hash_map.h: In method `hash_map<const unsigned char[8],int,hash<const unsigned char[8]>,kcmp,__default_alloc_template<false,0> >::hash_map<unsigned char[8], int, hash<const unsigned char[8]>, kcmp, alloc>()':
> /usr/include/g++-2/stl_hash_map.h:78: confused by earlier errors, bailing out
> uri@alisan:/usr2/src/crypto-3.0 > g++ -v
> Reading specs from /usr/lib/gcc-lib/i586-pc-linux-gnulibc1/egcs-2.91.60.1/specs
> gcc version egcs-2.91.60.1 19990115/Linux (egcs-1.1.1 release)
> uri@alisan:/usr2/src/crypto-3.0 >
>