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 

partial template specialisation error

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





PostPosted: Fri Feb 20, 2004 1:41 pm    Post subject: partial template specialisation error Reply with quote



Trying to get my head round partial template specialisation and
I'd be grateful for advice on this error; hope this isn't too
verbose.

-- Background --

I've a template for a hash table based on key-value pairs, where
the key must be an unsigned int. (The hash is done on the key and
the value gets put in the table.) Simplifying the name it looks like:

template < unsigned M, typename T >
class THashTable
....
int insert( unsigned key, const T & value );

where M is the size of the table and T is the type of object you want
to store in the table.

To avoid an ambiguity between overloads of the "search" method, type T
cannot be "unsigned". I want to do a partial specialisation of the
hash table to allow storage of unsigneds:

template < unsigned M >
class THashTable<M,unsigned>
....
int insert( unsigned value );

My intention was to implement this specialisation using a hash table to
store the unsigned, by having a dummy int value 0 as the T parameter and
using the value you want to store as the key:
private:
THashTable<M,int> m_Table;
....
insert(key, 0);


-- The Problem --

When I try compiling I get an error that looks like the compiler
doesn't spot that the 2 param insert above belongs to a THashTable<M,int>
but thinks it belongs to the specialisation, which only has
the one parameter insert, so it fails.

The listing below is an example with all the meat cut out of it
but the same structure as my code. It fails to compile, with the
following errors:

line 33: Error: Could not find a match for THashTable<M>::insert(unsigned, int).
line 40: Where: While instantiating
THashTable<73,unsigned>::insert(unsigned)
line 40: Where: Instantiated from non-template code.

Surely it ought to be ok to have a specialisation implementation that
uses a member variable of the type of the general template, and the
compiler ought to be able to spot that since m_Table is of type
THashTable<M,int> it should use that version of the function?

Geoff


1 #include <iostream.h>
2
3 template <unsigned M, typename T>
4 class THashTable
5 {
6 public:
7
8 int insert( unsigned, const T & );
9 };
10
11 template <unsigned M, typename T>
12 int
13 THashTable<M,T>::insert( unsigned , const T & )
14 {
15 return 0;
16 }
17
18 //specialisation
19 template < unsigned M >
20 class THashTable<M,unsigned>
21 {
22 public:
23 int insert ( unsigned );
24
25 private:
26 THashTable<M,int> m_Table;
27 };
28
29 template < unsigned M >
30 int
31 THashTable<M,unsigned>::insert( unsigned key )
32 {
33 return m_Table.insert(key, 0 ); // 0 is dummy
34 }
35
36 int main()
37 {
38 THashTable<73,unsigned> table;
39
40 cout << table.insert(10) << endl;
41 }

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Thomas Mang
Guest





PostPosted: Fri Feb 20, 2004 9:12 pm    Post subject: Re: partial template specialisation error Reply with quote





Geoff Macartney schrieb:

[problem snip]

Quote:

1 #include <iostream.h
2
3 template 4 class THashTable
5 {
6 public:
7
8 int insert( unsigned, const T & );
9 };
10
11 template 12 int
13 THashTable 14 {
15 return 0;
16 }
17
18 //specialisation
19 template < unsigned M
20 class THashTable 21 {
22 public:
23 int insert ( unsigned );
24
25 private:
26 THashTable 27 };
28
29 template < unsigned M
30 int
31 THashTable 32 {
33 return m_Table.insert(key, 0 ); // 0 is dummy
34 }
35
36 int main()
37 {
38 THashTable<73,unsigned> table;
39
40 cout << table.insert(10) << endl;
41 }

Your code looks fine to me.

What compiler are you using?


regards,

Thomas

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Geoff Macartney
Guest





PostPosted: Mon Feb 23, 2004 6:47 pm    Post subject: Re: partial template specialisation error Reply with quote



Thomas Mang <a9804814 (AT) unet (DOT) univie.ac.at> wrote

[....]
Quote:
Your code looks fine to me.

What compiler are you using?


regards,

Thomas

Forte 6.1 on Solaris 2.8

Does that code compile on your compiler?!?

thanks
Geoff

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Geoff Macartney
Guest





PostPosted: Mon Feb 23, 2004 6:51 pm    Post subject: Re: partial template specialisation error Reply with quote

Wait a moment, I have gcc somewhere on this box, let's try that...
well, blow me down, it works.

So is it maybe the compiler that's at issue, and not me? (That's a
first!)

I know that there are some issues with template support with Forte
6 (and its successors, I think) - e.g. it doesn't support
template template parameters. But I'd have thought this much should
be ok.

Geoff

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