Implicit and Explicit Template Instantiation

P

Pierre Yves

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
 
G

Guest

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.
 
R

Rolf Magnus

Pierre said:
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.
 
P

Pierre Yves

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:
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 said:
That's because it's not possible that way.

How can I solve that?
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.


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
 

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

No members online now.

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,805
Latest member
ClydeHeld1

Latest Threads

Top