473,506 Members | 14,630 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

template specialization of a template class

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
8 1055
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
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
The syntax looks like this
template<>
void classA<vector <Te> >::jump(vector<Te> _t)
{
}

Jul 23 '05 #4
sadhu wrote:
The syntax looks like this
template<>
void classA<vector <Te> >::jump(vector<Te> _t)
{
}


Really? What's 'Te'?
Jul 23 '05 #5
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
"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
> > 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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2524
by: SainTiss | last post by:
Hi, If you've got a template class with lots of methods, and then you've got a type which works with the template, except for one method... What you need to do there is specialize the...
8
7649
by: Agent Mulder | last post by:
Hi group, I have a problem with partial template specialization. In the code below I have a template struct Music with one method, play(), and three kinds of music, Jazz, Funk and Bach. When I...
2
5761
by: Jeff | last post by:
/* -------------------------------------------------------------------------- Hello, I was experimenting with class templates and specializing member functions and came across a simple problem...
9
2743
by: Marek Vondrak | last post by:
Hello. I have written the following program and am curious why it prints "1" "2". What are the exact effects of explicitly providing function template parameters at the call? Is the second...
2
2473
by: Thomas Kowalski | last post by:
Hi, I would like to write a template class Polygon<VertexTypthere vertex typ can be eigther a pointer or a value typ. It has an attribute: std::vector<VertexTypvertices; And a methode:...
9
3446
by: stephen.diverdi | last post by:
Can anyone lend a hand on getting this particular template specialization working? I've been trying to compile with g++ 4.1 and VS 2005. ...
2
7676
by: Barry | last post by:
The following code compiles with VC8 but fails to compiles with Comeau online, I locate the standard here: An explicit specialization of any of the following:
8
2934
by: flopbucket | last post by:
Hi, I want to provide a specialization of a class for any type T that is a std::map. template<typename T> class Foo { // ... };
1
2207
by: Ioannis Gyftos | last post by:
Hello, First the code :) /////////////////////////////////////////////////////////////////////////////////// // in another header file namespace LJC{
6
2583
by: abir | last post by:
i have a template as shown template<typename Sclass Indexer{}; i want to have a specialization for std::vector both const & non const version. template<typename T,typename Aclass...
0
7220
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
7023
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7479
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5617
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5037
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3188
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3178
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1534
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
410
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.