banner



Can A Template Class Have Virtual Functions

Thread: Pure virtual template function?

  1. #1

    Pure virtual template part?

    I've got a curious design problem I'm trying to figure out. Basically, I have a class called ModelSelector which has a function choose(). This function is pure virtual, the idea existence that different usages of the underlying framework might require different methods of model selection.

    The trouble is, different usages might *also* be passing different types of things into the choose() part to be modeled.

    I tin can't make ModelSelector a templated class because information technology needs to exist passed through the generic framework in several places, and there'due south no reasonable manner they could know the particular instantiation. So I idea mayhap if I made the cull() function itself templated, I could specialize it in the derived classes of ModelSelector. (It would only ever exist chosen from a derived class of a different base of operations, with a i-to-1 correspondence between the derived classes.)

    Still, this lawmaking:

    Lawmaking:

    class ModelSelector { public:     virtual ~ModelSelector() {}      template<typename T>     virtual boost::shared_ptr<Model> chooseModel(const T &data) const = 0; };
    is giving me a compile error. I'one thousand guessing templates and pure virtual functions don't mix well.

    To summarize: I accept base classes A and ModelSelector. A needs to know how to laissez passer a ModelSelector through its interface, simply only B derived from A and 1000 derived from ModelSelector know specifically what type of thing needs to be modeled.

    Any ideas on possible design improvements here?

    Last edited by Lindley; May 20th, 2009 at 01:22 PM.

  2. #ii

    Re: Pure virtual template function?

    I'm going to requite boost::whatever a try, come across if that is a reasonable arroyo.

  3. #3

    Re: Pure virtual template function?

    Pretty sure that virtual functions aren't allowed to exist templated (which makes sense when you think about it). Perhaps something similar this might practise what you're looking for?

    Code:

    typedef boost::shared_ptr<Model> ModelPtr;  class ModelSelector { protected:   template<typename T>   struct ModelChooser   {     ModelPtr operator()(const T& data);   };  public:   template<typename T>   ModelPtr chooseModel(const T& information)   {     render ModelChooser<T>( )(information);   } };  template<> struct ModelSelector::ModelChooser<char*> {   ModelPtr operator()(char* const& information)   {     // ...   } };

  4. #4

    Re: Pure virtual template function?

    Hmm. That's certainly an interesting idea.....presumably, wherever the function chooseModel() got chosen, it would be necessary to include the header containing the specialization, correct? Otherwise you lot'd become undefined symbol errors? I guess I have to determine whether or not that's adequate.

  5. #5

    Re: Pure virtual template function?

    You know, information technology simply crossed my mind... Why not just direct specialize the chooseModel office? The whole nested struct thing in my starting time is just a compeltely unnecessary extra layer. If you practise that then you should be fine with just having the specializations in one TU.

  6. #half dozen

    Re: Pure virtual template function?

    Quote Originally Posted by Lindley View Post

    I tin't make ModelSelector a templated class because it needs to be passed through the generic framework in several places, and at that place'southward no reasonable way they could know the particular instantiation.

    Sorry, only I don't become it. You lot'll be really be passing an already instantiated type, won't yous? Something like ModelSelector<Model>. So from the generic framework point of view it's merely a type. I assume in this instance that the generic framework likewise has templates.

    If I have understood it "backwards", in that location'due south still the possibility of parameterizing some classes that expect the model selector by template template parameters.

    Code:

    template <class T> grade ModelSelector { /*...*/ };  template <class ModelSelector_Type> class DoNotKnowInstantiation {   //... };  typedef ModelSelector<ModelX> ModelSelectorType; DoNotKnowInstantiation<ModelSelectorType> genFramework1;  template <template <course> class ModelSelector_Template> course KnowInstantiation {   typedef ModelSelector_Template<ModelW> ModelSelectorType;   //... };  KnowInstantiation<ModelSelector> genFramework2;

    Quote Originally Posted by Lindley View Post

    However, this code ... is giving me a compile error. I'm guessing templates and pure virtual functions don't mix well.

    Yes, templates and virtuals don't mix (even if they are not pure virtuals). Ane reason is that it would required too much work from the compiler/linker because it'south non possible to know in accelerate how many instantiantions a particular virtual template fellow member part (if it was possible) would exist. This imposes difficulties on constructing the virtual table.

  7. #7

    Re: Pure virtual template function?

    Quote Originally Posted by ltcmelo View Post

    Deplorable, only I don't become it. You'll be actually exist passing an already instantiated blazon, won't you? Something similar ModelSelector<Model>. So from the generic framework signal of view information technology'southward only a blazon. I presume in this case that the generic framework besides has templates.


    "Generic" may have been the wrong word for the framework. It mostly leverages inheritance-based polymorphism for now. I'thou not certain if that's the best blueprint, merely information technology'southward not obvious how templates can aid here at a higher level.

  8. #8

    Re: Pure virtual template function?

    Quote Originally Posted by Lindley View Post

    So I thought peradventure if I made the choose() function itself templated, I could specialize it in the derived classes of ModelSelector. (It would simply e'er be called from a derived class of a different base, with a i-to-one correspondence betwixt the derived classes.)

    That doesn't make much sense to me. Why exercise you lot need to invoke a pure virtual role in a base class if the information you need to pass to that function is dependent on the derived class. In gild words: why do you lot need a virtual function anyway?

    Quote Originally Posted by Lindley View Post

    To summarize: I take base classes A and ModelSelector. A needs to know how to laissez passer a ModelSelector through its interface, but only B derived from A and Grand derived from ModelSelector know specifically what type of thing needs to be modeled.

    So, does that mean that B can only piece of work with a ModelSelector that is actually an instance of Chiliad? In that case, B should assert that the ModelSelector it's getting from A is actually an case of M (or a derived class) and piece of work on that.

    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more than readable.

    Every bit long as homo ascribes to himself what is merely a posibility, he volition not work for the attainment of it. - P. D. Ouspensky


  9. #9

    Re: Pure virtual template function?

    Information technology's possible I *don't* need a virtual function. The blueprint is nonetheless evolving. I haven't figure out entirely yet whether ModelSelector needs to be visible to the non-specific part of the lawmaking or non yet.

  10. #10

    Re: Pure virtual template office?

    Mixture of compile time and run time characteristic seem very strange to me.

    CT POlymorphism or Policy Based Design Or Traits.

    Thanks for your help.


Posting Permissions

  • Yous may not post new threads
  • Y'all may not postal service replies
  • You may not mail attachments
  • You may not edit your posts
  • BB code is On
  • Smilies are On
  • [IMG] lawmaking is On
  • [VIDEO] code is On
  • HTML code is Off

Click Here to Expand Forum to Full Width



Can A Template Class Have Virtual Functions,

Source: https://forums.codeguru.com/showthread.php?477467-Pure-virtual-template-function

Posted by: formanthoulace.blogspot.com

0 Response to "Can A Template Class Have Virtual Functions"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel