This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC 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]

G++ 3.3.1 Specialization bug?


The following piece of code seems to reveal a bug in GCC 3.3.1 (and
maybe other GCC 3.x compilers):

#include <stddef.h>
#if __GNUC__ == 3 && __GNUC_MINOR__ > 0
#include <ext/hash_map>
#else
#include <hash_map>
#define __gnu_cxx std
#endif

struct my_obj {
    int    _value;
    my_obj(int v) : _value(v) {}
};

template <> struct __gnu_cxx::hash<my_obj> {
    size_t operator()(const my_obj& r) {
        return r._value;
    }
};

int main()
{
    __gnu_cxx::hash_map<my_obj, int> a;
}

The program above will compile under GCC 2.95, but not under GCC 3.3.1.
The error message is:

test1.cpp:14: error: specializing `struct __gnu_cxx::hash<my_obj>' in
different namespace
D:/mingw-gcc3/include/c++/3.3.1/ext/stl_hash_fun.h:71: error:   from
definition
   of `template<class _Key> struct __gnu_cxx::hash'

It will work if I put the specialization code in "namespace __gnu_cxx {
... }".  I suppose both forms should be allowed, according to the C++
standard (14.7.3.9).

Another problem is libstdc++ related.  According to its policy of
putting SGI extensions in __gnu_cxx, I should rewrap them in a namespace
like "sgi" to use them in different compilers.  But the following piece
of code does not work in either GCC 2.95.3 or GCC 3.3.1:

#include <stddef.h>
#if __GNUC__ == 3 && __GNUC_MINOR__ > 0
#include <ext/hash_map>
namespace sgi {
    using __gnu_cxx::hash;
    using __gnu_cxx::hash_map;
}
#else
#include <hash_map>
namespace sgi {
    using std::hash;
    using std::hash_map;
}
#endif

struct my_obj {
    int _value;
    my_obj(int v) : _value(v) {}
};

namespace sgi {
    template <> struct hash<my_obj> {
        size_t operator()(const my_obj& r) {
            return r._value;
        }
    };
}

int main()
{
    sgi::hash_map<my_obj, int> a;
}

This is really making life difficult when hash_map support is wanted.

Any ideas?

Best regards,

Wu Yongwei



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