 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Colin Wills Guest
|
Posted: Tue Mar 22, 2005 2:01 am Post subject: Partial specialisation and a template template parameter |
|
|
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
|
Posted: Tue Mar 22, 2005 9:30 am Post subject: Re: Partial specialisation and a template template parameter |
|
|
"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
|
Posted: Tue Mar 22, 2005 10:45 pm Post subject: Re: Partial specialisation and a template template parameter |
|
|
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 |
|
 |
|
|
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
|
|