Errors when typdef'ing hash_map::iterator

Matt Fischer mattfischer84@gmail.com
Mon Apr 16 20:30:00 GMT 2007


Hi all.  I've run into a problem using the hash_map extension class in
libstdc++, and I'm evidently not well-versed enough in templates to
determine what the root cause is.  Essentially the problem I'm seeing
arises when I attempt to make a typedef of a hash_map iterator which
references a forward-declared class.  Below is a simplified example
that triggers the behavior:

----------------------------------------------------------------------
#include <map>
#include <string>
#include <ext/hash_map>

class Foo;

typedef std::map<int, Foo>::iterator FooMapIt; // This works
typedef __gnu_cxx::hash_map<int, Foo>::iterator FooHashMapIt; //
****** This doesn't

class Foo
{
    public:
        Foo() {}
        ~Foo() {}
};

int main(int argc, char *argv[])
{
    return 0;
}
----------------------------------------------------------------------


I'm porting this code in from a Visual Studio project, where it
presumably compiles just fine.  On GCC, though, I get the following
errors:

----------------------------------------------------------------------
/usr/lib/gcc/i486-linux-gnu/4.0.3/../../../../include/c++/4.0.3/bits/stl_pair.h:
In instantiation of 'std::pair<const int, Foo>':
 /usr/lib/gcc/i486-linux-gnu/4.0.3/../../../../include/c++/4.0.3/bits/stl_function.h:544:
  instantiated from 'std::_Select1st<std::pair<const int, Foo> >'
/usr/lib/gcc/i486-linux-gnu/4.0.3/../../../../include/c++/4.0.3/ext/hashtable.h:306:
  instantiated from '__gnu_cxx::hashtable<std::pair<const int, Foo>,
int, __gnu_cxx::hash<int>, std::_Select1st<std::pair<const int, Foo>
>, std::equal_to<int>, std::allocator<Foo> >'
/usr/lib/gcc/i486-linux-gnu/4.0.3/../../../../include/c++/4.0.3/ext/hash_map:99:
  instantiated from '__gnu_cxx::hash_map<int, Foo,
__gnu_cxx::hash<int>, std::equal_to<int>, std::allocator<Foo> >'
test.cc:8:   instantiated from here
/usr/lib/gcc/i486-linux-gnu/4.0.3/../../../../include/c++/4.0.3/bits/stl_pair.h:74:
error: 'std::pair<_T1, _T2>::second' has incomplete type
test.cc:5: error: forward declaration of 'struct Foo'
----------------------------------------------------------------------

The compiler is GCC 4.0.3 (stock Ubuntu Dapper system.)

I'm not an expert on templates, and I know that lots of strange,
non-obvious errors can result when they're misused.  However, the fact
that this works with a std::map and fails on a hash_map suggests to me
that there are no syntactic problems here, and that this is actually
due to something within the implementation of hash_map itself.  Can
anybody verify this, or alternatively, point out some coding error
that I've missed that will correct this problem?

Thanks,
Matt



More information about the Libstdc++ mailing list