Virtual Template or Template Virtual

Discussion in 'C++' started by pocmatos@gmail.com, Jan 23, 2006.

  1. Guest

    Hi all,

    I have an abstract class acting as interface to a given class of
    objects. And mostly everywhere around my program I'm passing things
    like: vector<int>, list<unsigned long>, list<myclass*>, just to access
    the elements it contains. Today I decided to refactor and to try
    templates.
    So I have abstract class A with function foo which receives
    list<myclass*>. And now I want it to receive an Input Iterator
    (following the idea behind <algorithm>). So I try this:
    template<class InputIterator>
    virtual void foo(InputIterator first, InputIterator last) = 0;

    Then I have B inheriting from A, defining the function foo. However it
    seems the compiler doesn't like template<...> virtual... I also tried
    the other way around (virtual template...) but it is of no use.

    Any ideas on how to do this is _extremely_ welcome.

    Oh, by the way... "Effective C++" book is on its way... Heard great
    things about it. Hope its true!

    Cheers,

    Paulo Matos
    , Jan 23, 2006
    #1
    1. Advertising

  2. wrote:
    > I have an abstract class acting as interface to a given class of
    > objects. And mostly everywhere around my program I'm passing things
    > like: vector<int>, list<unsigned long>, list<myclass*>, just to access
    > the elements it contains. Today I decided to refactor and to try
    > templates.
    > So I have abstract class A with function foo which receives
    > list<myclass*>. And now I want it to receive an Input Iterator
    > (following the idea behind <algorithm>). So I try this:
    > template<class InputIterator>
    > virtual void foo(InputIterator first, InputIterator last) = 0;


    Can't do that. A virtual function cannot be a template. Or vice versa.

    > Then I have B inheriting from A, defining the function foo. However it
    > seems the compiler doesn't like template<...> virtual... I also tried
    > the other way around (virtual template...) but it is of no use.


    Yes. Prohibited.

    > Any ideas on how to do this is _extremely_ welcome.


    Don't make it a template or don't make it virtual.

    > Oh, by the way... "Effective C++" book is on its way... Heard great
    > things about it. Hope its true!


    It's not bad.

    V
    Victor Bazarov, Jan 23, 2006
    #2
    1. Advertising

  3. mlimber Guest

    Victor Bazarov wrote:
    > wrote:
    > > I have an abstract class acting as interface to a given class of
    > > objects. And mostly everywhere around my program I'm passing things
    > > like: vector<int>, list<unsigned long>, list<myclass*>, just to access
    > > the elements it contains. Today I decided to refactor and to try
    > > templates.
    > > So I have abstract class A with function foo which receives
    > > list<myclass*>. And now I want it to receive an Input Iterator
    > > (following the idea behind <algorithm>). So I try this:
    > > template<class InputIterator>
    > > virtual void foo(InputIterator first, InputIterator last) = 0;

    >
    > Can't do that. A virtual function cannot be a template. Or vice versa.
    >
    > > Then I have B inheriting from A, defining the function foo. However it
    > > seems the compiler doesn't like template<...> virtual... I also tried
    > > the other way around (virtual template...) but it is of no use.

    >
    > Yes. Prohibited.
    >
    > > Any ideas on how to do this is _extremely_ welcome.

    >
    > Don't make it a template or don't make it virtual.
    >
    > > Oh, by the way... "Effective C++" book is on its way... Heard great
    > > things about it. Hope its true!

    >
    > It's not bad.
    >
    > V


    Victor is correct, as usual. However, see this old post for an
    alternative approach that may suit you:

    http://groups.google.com/group/comp.lang.c /msg/c2cfcf8fcfd7b913

    Cheers! --M
    mlimber, Jan 23, 2006
    #3
  4. mlimber wrote:
    > Victor Bazarov wrote:
    >
    >> wrote:
    >>
    >>>I have an abstract class acting as interface to a given class of
    >>>objects. And mostly everywhere around my program I'm passing things
    >>>like: vector<int>, list<unsigned long>, list<myclass*>, just to access
    >>>the elements it contains. Today I decided to refactor and to try
    >>>templates.
    >>>So I have abstract class A with function foo which receives
    >>>list<myclass*>. And now I want it to receive an Input Iterator
    >>>(following the idea behind <algorithm>). So I try this:
    >>>template<class InputIterator>
    >>>virtual void foo(InputIterator first, InputIterator last) = 0;

    >>
    >>Can't do that. A virtual function cannot be a template. Or vice versa.
    >>
    >>
    >>>Then I have B inheriting from A, defining the function foo. However it
    >>>seems the compiler doesn't like template<...> virtual... I also tried
    >>>the other way around (virtual template...) but it is of no use.

    >>
    >>Yes. Prohibited.
    >>
    >>
    >>>Any ideas on how to do this is _extremely_ welcome.

    >>
    >>Don't make it a template or don't make it virtual.
    >>
    >>
    >>>Oh, by the way... "Effective C++" book is on its way... Heard great
    >>>things about it. Hope its true!

    >>
    >>It's not bad.
    >>
    >>V

    >
    >
    > Victor is correct, as usual. However, see this old post for an
    > alternative approach that may suit you:
    >
    > http://groups.google.com/group/comp.lang.c /msg/c2cfcf8fcfd7b913


    I am not sure how this solves the OP's problem. Notice, I didn't say it
    didn't solve it. I just said I wasn't sure. Mostly because the OP didn't
    actually say what problem (aside from unifying passing different types of
    containers around) he's trying to solve.

    It is possible that the whole class needs to become a template.

    It is also possible that those virtual functions simply need to be
    overloaded based on the type (and thus the need in templates will vanish).
    Perhaps the OP can still achieve what he needs by supplying a limited set
    of functions...

    virtual void foo(list<int>::iterator ...
    virtual void foo(vector<unsigned long>::iterator ...
    virtual void foo(list<myclass*>::iterator ...

    V
    Victor Bazarov, Jan 23, 2006
    #4
  5. mlimber Guest

    Victor Bazarov wrote:
    > mlimber wrote:
    > > Victor Bazarov wrote:
    > >
    > >> wrote:
    > >>
    > >>>I have an abstract class acting as interface to a given class of
    > >>>objects. And mostly everywhere around my program I'm passing things
    > >>>like: vector<int>, list<unsigned long>, list<myclass*>, just to access
    > >>>the elements it contains. Today I decided to refactor and to try
    > >>>templates.
    > >>>So I have abstract class A with function foo which receives
    > >>>list<myclass*>. And now I want it to receive an Input Iterator
    > >>>(following the idea behind <algorithm>). So I try this:
    > >>>template<class InputIterator>
    > >>>virtual void foo(InputIterator first, InputIterator last) = 0;
    > >>
    > >>Can't do that. A virtual function cannot be a template. Or vice versa.
    > >>
    > >>
    > >>>Then I have B inheriting from A, defining the function foo. However it
    > >>>seems the compiler doesn't like template<...> virtual... I also tried
    > >>>the other way around (virtual template...) but it is of no use.
    > >>
    > >>Yes. Prohibited.
    > >>
    > >>
    > >>>Any ideas on how to do this is _extremely_ welcome.
    > >>
    > >>Don't make it a template or don't make it virtual.
    > >>
    > >>
    > >>>Oh, by the way... "Effective C++" book is on its way... Heard great
    > >>>things about it. Hope its true!
    > >>
    > >>It's not bad.
    > >>
    > >>V

    > >
    > >
    > > Victor is correct, as usual. However, see this old post for an
    > > alternative approach that may suit you:
    > >
    > > http://groups.google.com/group/comp.lang.c /msg/c2cfcf8fcfd7b913

    >
    > I am not sure how this solves the OP's problem. Notice, I didn't say it
    > didn't solve it. I just said I wasn't sure. Mostly because the OP didn't
    > actually say what problem (aside from unifying passing different types of
    > containers around) he's trying to solve.
    >
    > It is possible that the whole class needs to become a template.
    >
    > It is also possible that those virtual functions simply need to be
    > overloaded based on the type (and thus the need in templates will vanish).
    > Perhaps the OP can still achieve what he needs by supplying a limited set
    > of functions...
    >
    > virtual void foo(list<int>::iterator ...
    > virtual void foo(vector<unsigned long>::iterator ...
    > virtual void foo(list<myclass*>::iterator ...
    >
    > V


    Again, Victor is correct. There are a number of potential options, but
    we can't choose between them based on the information given.

    Cheers! --M
    mlimber, Jan 23, 2006
    #5
    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. Chris Theis
    Replies:
    2
    Views:
    454
    Chris Theis
    Jul 24, 2003
  2. tom_usenet
    Replies:
    0
    Views:
    524
    tom_usenet
    Jul 24, 2003
  3. Replies:
    2
    Views:
    537
  4. Replies:
    1
    Views:
    2,084
    Gianni Mariani
    Jun 8, 2007
  5. Peng Yu
    Replies:
    3
    Views:
    754
    Thomas J. Gritzan
    Oct 26, 2008
Loading...

Share This Page