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

Implicit and Explicit Template Instantiation

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a

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 discussion thread is closed

Replies have been disabled for this discussion.