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 specialisation and a template template parameter

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





PostPosted: Tue Mar 22, 2005 2:01 am    Post subject: Partial specialisation and a template template parameter Reply with quote



I am trying to get the compiler to specialise a general purpose lookup
object based on maps to use arrays when a reasonable int range is the
lookup parameter. (This is a simplified extract of what I'm trying to
do.) I want the int range and decision to use arrays to be worked out
at compile time but gcc doesn't like the ERROR commented row below.
What's the right syntax for this partial specialisation with a template
template parameter?

#include <iostream>
#include <map>
#include <string>

using namespace std;

template <typename RT, typename LT>
class Table
{
map<LT,RT> m_data;
public:
void add(LT param, RT value) {m_data[param] = value;}
RT& operator[] (LT param) {return m_data[param];}
};

template <int S>
class IntRange
{
enum {size = S};
};

template <typename RT>
class Table<RT, template
// ERROR: syntax error before `template'
{
RT m_data[IntRange::size];
public:
void add(int param, RT value) {m_data[param] = value;}
RT operator[] (int param) {return m_data[param];}
};

int main()
{
Table<double,string> t1;
t1.add(string("fred"),42.5);
cout << "t1[fred]=" << t1[string("fred")] << endl;

// Table t2;
// t2.add(4,3.14);
// cout << "t2[4]=" << t2[4] << endl;
}


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





PostPosted: Tue Mar 22, 2005 9:30 am    Post subject: Re: Partial specialisation and a template template parameter Reply with quote



"Colin Wills" <colin (AT) nonhtmlmail (DOT) org> writes:

Quote:
What's the right syntax for this partial specialisation with a
template template parameter?

What you actually want is a partial specialization with a template
*instantiation*, I think.


Quote:
#include <iostream

#include
Quote:
#include <map
#include
using namespace std;

template class Table
{
map public:
void add(LT param, RT value) {m_data[param] = value;}
RT& operator[] (LT param) {return m_data[param];}
};

template <int S
class IntRange
{
enum {size = S};
};

template class Table

This is not compatible with the primary template. The primary template
takes two parameters of type type, while this partial specialization
takes a parameter of type type and one of type template.

This

template <typename RT, int S>
class Table<RT, IntRange

is compatible with the primary template.


Quote:
// ERROR: syntax error before `template'
{
RT m_data[IntRange::size];

RT m_data[S];

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

Back to top
Maxim Yegorushkin
Guest





PostPosted: Tue Mar 22, 2005 10:45 pm    Post subject: Re: Partial specialisation and a template template parameter Reply with quote



Colin Wills <colin (AT) nonhtmlmail (DOT) org> wrote:

Quote:
template <typename RT
class Table // ERROR: syntax error before `template'
{
RT m_data[IntRange::size];
public:
void add(int param, RT value) {m_data[param] = value;}
RT operator[] (int param) {return m_data[param];}
};

There is just a little syntax error. Fix:

template <typename RT, int S>
class Table<RT, IntRange
{
RT m_data[S];
// as before
};

--
Maxim Yegorushkin

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