By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 913 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

template specialization of a template class

P: n/a
I dare to bother you again.
Given a template class:

template <typename T>
class ClassA {
T t;
public:
void jump(T _t);
void die();
};

I need a specialization of this class to provide different implementation of
jump() method for realizations of this class with type vector<Te> where Te
is an arbitrary type.
Unfortunatelly this does not work (compile):

template <typename Te>
class classA< vector<Te> > {
vector<Te> t;
public:
void jump(vector<Te> _t);
};

How can I do it?

--
pit3k
Jul 23 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Given a template class:

template <typename T>
class ClassA {
T t;
public:
void jump(T _t);
void die();
};

I need a specialization of this class to provide different implementation
of jump() method for realizations of this class with type vector<Te> where
Te is an arbitrary type.
Unfortunatelly this does not work (compile):

template <typename Te>
class classA< vector<Te> > {
vector<Te> t;
public:
void jump(vector<Te> _t);
};


You misspelled the name of the class in the partial specialization as
'classA'.
Jul 23 '05 #2

P: n/a
pit3k wrote:
I dare to bother you again.
Given a template class:

template <typename T>
class ClassA {
T t;
public:
void jump(T _t);
void die();
};

I need a specialization of this class to provide different implementation of
jump() method for realizations of this class with type vector<Te> where Te
is an arbitrary type.
Unfortunatelly this does not work (compile):

template <typename Te>
class classA< vector<Te> > {
vector<Te> t;
public:
void jump(vector<Te> _t);
};

How can I do it?


You must have made some other mistake. This:
-------------
#include <vector>
#include <iostream>

template<class T> struct S {
void foo(T t) { std::cout << "Generic\n"; }
};

template<class T> struct S<std::vector<T> > {
void foo(std::vector<T> vt) { std::cout << "Specialised\n"; }
};

int main() {
std::vector<int> vi;
S<double> sd;
sd.foo(3.1415926);
S<std::vector<int> > svi;
svi.foo(vi);
}
--------------
is valid code and compiles and even outputs what I'd expect.

V
Jul 23 '05 #3

P: n/a
The syntax looks like this
template<>
void classA<vector <Te> >::jump(vector<Te> _t)
{
}

Jul 23 '05 #4

P: n/a
sadhu wrote:
The syntax looks like this
template<>
void classA<vector <Te> >::jump(vector<Te> _t)
{
}


Really? What's 'Te'?
Jul 23 '05 #5

P: n/a
That was a gross mistake on my part. I just didn't realize there was a
type parameter ('Te') involved.

Regards
Senthil

Jul 23 '05 #6

P: n/a
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:ME*******************@newsread1.mlpsca01.us.t o.verio.net...
pit3k wrote:

template <typename T>
class ClassA {
T t;
public:
void jump(T _t);
void die();
};

I need a specialization of this class to provide different implementation
of jump() method for realizations of this class with type vector<Te>
where Te is an arbitrary type.
Unfortunatelly this does not work (compile):

template <typename Te>
class classA< vector<Te> > {
vector<Te> t;
public:
void jump(vector<Te> _t);
};
You must have made some other mistake.


you are, of course, right.
in my original code i omited the darn 'typename' keyword at specialization
definition :/
This:
-------------
#include <vector>
#include <iostream>

template<class T> struct S {
void foo(T t) { std::cout << "Generic\n"; }
};

template<class T> struct S<std::vector<T> > {
void foo(std::vector<T> vt) { std::cout << "Specialised\n"; }
};

int main() {
std::vector<int> vi;
S<double> sd;
sd.foo(3.1415926);
S<std::vector<int> > svi;
svi.foo(vi);
}
--------------
is valid code and compiles and even outputs what I'd expect.


that is exactly what i want and it indeed does work
thank you

--
pit3k
Jul 23 '05 #7

P: n/a
> > template <typename Te>
class classA< vector<Te> > { // 2nd line
vector<Te> t;
public:
void jump(vector<Te> _t);
};


basic question: what doeas 2nd line mean (class classA< vector<Te> >
{)?

can't quite recalle that classA <vector<Te> >{... meaning.
thanks...

Jul 23 '05 #8

P: n/a
"puzzlecracker" <ir*********@gmail.com> wrote...
> template <typename Te>
> class classA< vector<Te> > { // 2nd line
> vector<Te> t;
> public:
> void jump(vector<Te> _t);
> };


basic question: what doeas 2nd line mean (class classA< vector<Te> >
{)?

can't quite recalle that classA <vector<Te> >{... meaning.


The OP's intention was to [partially] specialise the initial template
for a vector of Te. IOW, for any non-vector template argument the
initial template is to be used and for any argument that itself is
a vector of something the second, special template should be used.

V
Jul 23 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.