This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
G++ 3.3.1 Specialization bug?
- From: Wu Yongwei <adah at netstd dot com>
- To: gcc at gcc dot gnu dot org, libstdc++ at gcc dot gnu dot org
- Date: Sat, 30 Aug 2003 12:53:26 +0800
- Subject: G++ 3.3.1 Specialization bug?
- Organization: Kingnet Security, Inc.
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