Implicit and Explicit Template Instantiation

Discussion in 'C++' started by Pierre Yves, Nov 1, 2007.

  1. Pierre Yves

    Pierre Yves 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();

    Regards,
    PY
     
    Pierre Yves, Nov 1, 2007
    #1
    1. Advertising

  2. 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
     
    =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=, Nov 1, 2007
    #2
    1. Advertising

  3. Pierre Yves

    Rolf Magnus Guest

    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.
     
    Rolf Magnus, Nov 1, 2007
    #3
  4. Pierre Yves

    Pierre Yves Guest

    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<T> is 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
     
    Pierre Yves, Nov 1, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. C. Carbonera
    Replies:
    4
    Views:
    1,348
    C. Carbonera
    Feb 5, 2004
  2. Fernando Cuenca
    Replies:
    4
    Views:
    2,528
    Gianni Mariani
    Sep 6, 2004
  3. Thomas Maier-Komor
    Replies:
    6
    Views:
    627
    Thomas Maier-Komor
    May 19, 2005
  4. Replies:
    1
    Views:
    579
    Salt_Peter
    Dec 25, 2006
  5. Noah Roberts
    Replies:
    6
    Views:
    1,169
    Johannes Schaub (litb)
    Feb 2, 2011
Loading...

Share This Page