473,320 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Implicit and Explicit Template Instantiation

Hi there,

Template, once again... Nice topic but a bit tricky to code.

My problem is the following:

I have to video parser which retrieve image frames. The pixel values can
be unsigned char (8 bit) or unsigned short (16 bit).

I've got a templated frame:

template <class T>
class Frame {
Frame(int height, int width) {
data = new T[height * width]
}
T * data;
}

I've got my readers:

class Video16bit : public Frame<unsigned char{
Video16bit : Frame(20,20) {
// whatever
}
}

class Video16bit : public Frame<unsigned short{
Video16bit : Frame(20,20) {
// whatever
}
}

This one seems to compile fine. Everything goes wrong when I try to
process these videos.

I would like to create a class which can take either of the videos but I
got lost...

class Processor {
public:
Processor();
LoadData(Frame<T* data);
protected:
Frame<T* mydata;
}

My main would look like (the two videos should be usable):

int main(void) {
Video16bit * vid = new Video16bit();
// Video8bit * vid = new Video8bit();
Processor * pro = new Processor();
pro->LoadData(vid);
delete whatever is required;
}

The problem is that the creation of my processor depends on the type of
template and ... I dunno how to pass it cleanly. My current solution is
to template the Processor as well. However, I could make a mistake of
type: I would like the T of the processor defined by the loaded video...

Does it make sense?

Another way to see it would be: can I do a
Processor<vid->getType()* pro = new Processor();

Regards,
PY
Nov 1 '07 #1
3 2827
On 2007-11-01 11:46, Pierre Yves wrote:
Hi there,

Template, once again... Nice topic but a bit tricky to code.

My problem is the following:

I have to video parser which retrieve image frames. The pixel values can
be unsigned char (8 bit) or unsigned short (16 bit).

I've got a templated frame:

template <class T>
class Frame {
Frame(int height, int width) {
data = new T[height * width]
}
T * data;
}

I've got my readers:

class Video16bit : public Frame<unsigned char{
Video16bit : Frame(20,20) {
// whatever
}
}

class Video16bit : public Frame<unsigned short{
Video16bit : Frame(20,20) {
// whatever
}
}

This one seems to compile fine. Everything goes wrong when I try to
process these videos.

I would like to create a class which can take either of the videos but I
got lost...

class Processor {
public:
Processor();
LoadData(Frame<T* data);
protected:
Frame<T* mydata;
}

My main would look like (the two videos should be usable):

int main(void) {
Video16bit * vid = new Video16bit();
// Video8bit * vid = new Video8bit();
Processor * pro = new Processor();
pro->LoadData(vid);
delete whatever is required;
}

The problem is that the creation of my processor depends on the type of
template and ... I dunno how to pass it cleanly. My current solution is
to template the Processor as well. However, I could make a mistake of
type: I would like the T of the processor defined by the loaded video...

Does it make sense?

Another way to see it would be: can I do a
Processor<vid->getType()* pro = new Processor();
That will not work, a function can not return a type. The solution is to
add a typedef to Frame:

template <class T>
class Frame {
public:
typedef T Type;
Frame(int height, int width) {
data = new T[height * width];
}
T * data;
};

And then make Processor a template class like you said and use the
typedef when instantiating it.

int main()
{
Video16bit * vid = new Video16bit();
//Video8bit * vid = new Video8bit();
Processor<Video16bit::Type>* pro = new Processor<Video16bit::Type>();
pro->LoadData(vid);
}

By the way, you can not use the wrong type, the compiler will complain
if you try.

--
Erik Wikström
Nov 1 '07 #2
Pierre Yves wrote:
I would like to create a class which can take either of the videos but I
got lost...

class Processor {
public:
Processor();
LoadData(Frame<T* data);
protected:
Frame<T* mydata;
}
What's T?
My main would look like (the two videos should be usable):

int main(void) {
Video16bit * vid = new Video16bit();
// Video8bit * vid = new Video8bit();
Processor * pro = new Processor();
pro->LoadData(vid);
delete whatever is required;
}

The problem is that the creation of my processor depends on the type of
template and ... I dunno how to pass it cleanly.
That's because it's not possible that way.
My current solution is to template the Processor as well. However, I could
make a mistake of type: I would like the T of the processor defined by the
loaded video...
Does it make sense?
Yes, it does, but then you need polymorphism. Make a common base class with
the functions that differ virtual. Derive your two video classes from it.
Another way to see it would be: can I do a
Processor<vid->getType()* pro= new Processor();
No, you can't. Template arguments are fixed at compile time.

Nov 1 '07 #3

First of all, the solution of Erik works. You still have to know the
type of data you're loading but you don't need to know the internal
storage type.
Rolf Magnus previously wrote the following e-mail:
Pierre Yves wrote:
>I would like to create a class which can take either of the videos but I
got lost...

class Processor {
public:
Processor();
LoadData(Frame<T* data);
protected:
Frame<T* mydata;
}

What's T?
That's the problem. How can my processor type be compiled with the
proper type, when I pass it later on.

Frame<Tis the base class of data which is either video type.
>
>My main would look like (the two videos should be usable):

int main(void) {
Video16bit * vid = new Video16bit();
// Video8bit * vid = new Video8bit();
Processor * pro = new Processor();
pro->LoadData(vid);
delete whatever is required;
}

The problem is that the creation of my processor depends on the type of
template and ... I dunno how to pass it cleanly.

That's because it's not possible that way.
How can I solve that?
>My current solution is to template the Processor as well. However, I could
make a mistake of type: I would like the T of the processor defined by the
loaded video...
Does it make sense?

Yes, it does, but then you need polymorphism. Make a common base class with
the functions that differ virtual. Derive your two video classes from it.
>Another way to see it would be: can I do a
Processor<vid->getType()* pro= new Processor();

No, you can't. Template arguments are fixed at compile time.
I know. However, there should be a way. I know that I should be able to
do that...

Basically, working on that, I reckon that having a templated member
force me to template the whole class...

Thanks Erik and Rolf for you help.

PY

Nov 1 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: C. Carbonera | last post by:
/* Hi, I have a problem with explicit instantiation of templates in Visual C++ 6.0. I have provided the source below. I have an example of a function template that produces incorrect output in...
4
by: CoolPint | last post by:
I would be grateful if someone could point out if I am understanding correctly and suggest ways to improve. Sorry for the long message and I hope you will kindly bear with it. I have to make it...
11
by: Johan | last post by:
Hi Can somebody explain to me why I get this warning message and how I can solve this warning message. Thanks a lot Johan In member function `void
5
by: Andy | last post by:
Hi, I got a little confused on 'instantiation' and 'specialization', espcially for explicit instantiation and explicit sepcialization. Can anybody explain the difference? Thanks a lot! ...
6
by: Thomas Maier-Komor | last post by:
Hi everybody, I am a little bit confused with the syntax of explicit instantiation, and I am not sure that it is possible to do what I want to do. Maybe someone has an idea. Consider a...
3
by: Dilip | last post by:
I am stumbling my way through C++ templates and I keep running into way too many questions. Here is one: If a library writer exposes a function template like so: template<typename T> void...
1
by: krunalbauskar | last post by:
Hi, Explicit instantiation of STL vector demands explicit instantiation of all the templates it using internally. For example - <snippet> #include <iostream> #include <vector>
4
by: yuanhp_china | last post by:
I define a class in A.h: template <class Tclass A{ public: void get_elem( const T&) ; };
0
by: greek_bill | last post by:
Hi, I have a template function for which I use SFINAE to restrict one of the parameters. Then I also have a partial specialization of this function.I would like to provide an explicit...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.