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]

Re: G++ 3.3.1 Specialization bug?


In answer to the first part of your question:

On Sat, Aug 30, 2003 at 12:53:26PM +0800, Wu Yongwei wrote:

> 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).


GCC is correct as I understand it. The form you tried declares a
specialisation of __gnu_cxx::hash in the global namespace (not in the
__gnu_cxx namespace). Specialisations must be in the same namespace as
the original template, so you have to say:

namespace __gnu_cxx {
    // specialisation
}

I assume the code works with GCC 2.95 because you've defined __gnu_cxx
as std, and because in 2.95 namespace std is ignored std::hash is
really in the global namespace, so your explicit specialisation in the
global namespace is OK.

jon


-- 
"Anybody who wants the presidency so much that he'll spend two years
 organizing and campaigning for it is not to be trusted with the office."
	- David Broder


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