C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

template friends, specialization usage change with gcc 4.0.1

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Tom Mander
Guest





PostPosted: Sun Jun 04, 2006 5:22 am    Post subject: template friends, specialization usage change with gcc 4.0.1 Reply with quote



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! ]
Back to top
Valentin Samko
Guest





PostPosted: Sun Jun 04, 2006 6:17 pm    Post subject: Re: template friends, specialization usage change with gcc 4 Reply with quote



Tom Mander wrote:
Quote:
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
};

You are missing some code here. Where is the hashFunc template function declared? The same
for the non template hashFunc function. It is very hard to tell the problem without seeing
the code that you are trying to compile. Most likely the difference in behaviour is due to
the two phase template instantiation done by gcc4, where it "parses" templates in two
phases, and not only when they are instantiated (like gcc3).

--

Valentin Samko - http://www.valentinsamko.com

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.