template friends, specialization usage change with gcc 4.0.1
Hi,
I've found a solution to my problem, but I'm interested in
understanding more of what I am seeing in a slightly obtuse symbol
binding issue. This code was written a long time ago by someone else.
I'd be interested in any insight into what gcc4 (4.0.1) is doing and
why it is different to gcc3, and confirming that what I am seeing is
valid behaviour (i.e. more ANSI standards compliant) rather than a
compiler bug.
Take templated class Hash:
template <class K, class V>
class Hash
{
public:
enum { DEFAULT_SIZE = 64 };
typedef unsigned int (*HashFunc)(const K&);
Hash(int size = DEFAULT_SIZE, HashFunc = hashFunc);
...
};
class A {
...
friend unsigned int hashFunction(const A&); //used by Hash::Hash in
gcc3
friend unsigned int hashFunc<A>(const A&); //used by Hash::Hash in
gcc4
};
class B {
...
Hash<A, B*> _children;
...
};
To successfully link using gcc3, I need the following defined:
unsigned int hashFunc(const A& a)
{
...
}
While for gcc4, I need:
template<> unsigned int hashFunc<A>(const A& a)
{
...
}
My gut tells me that what gcc4 requires looks more correct, but nm
shows that gcc3 definitely binds to the former, and gcc4 to the latter.
Gcc4 seems to be implicitly using the fully specialized friend template
method, whereas gcc3 looks for a non-template friend.
Any comments welcome.
Tom
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]