template specialization of a template class

P

pit3k

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?
 
K

Kurt Krueckeberg

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'.
 
V

Victor Bazarov

pit3k said:
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);
}
 
S

sadhu

The syntax looks like this
template<>
void classA<vector <Te> >::jump(vector<Te> _t)
{
}
 
S

sadhu

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

Regards
Senthil
 
P

pit3k

Victor Bazarov said:
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);
}

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

puzzlecracker

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

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


thanks...
 
V

Victor Bazarov

puzzlecracker said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top