banner



Can Template Function Be Virtual

Thread: Pure virtual template function?

  1. #1

    Pure virtual template function?

    I've got a curious design problem I'chiliad trying to figure out. Basically, I accept a class called ModelSelector which has a function choose(). This function is pure virtual, the idea beingness that different usages of the underlying framework might crave different methods of model option.

    The problem is, different usages might *likewise* be passing dissimilar types of things into the cull() office to be modeled.

    I can't make ModelSelector a templated class because information technology needs to be passed through the generic framework in several places, and in that location'south no reasonable mode they could know the item instantiation. So I thought maybe if I made the choose() office itself templated, I could specialize it in the derived classes of ModelSelector. (It would only ever be called from a derived class of a different base, with a ane-to-1 correspondence betwixt the derived classes.)

    However, this code:

    Lawmaking:

    form ModelSelector { public:     virtual ~ModelSelector() {}      template<typename T>     virtual heave::shared_ptr<Model> chooseModel(const T &information) const = 0; };
    is giving me a compile fault. I'm guessing templates and pure virtual functions don't mix well.

    To summarize: I take base classes A and ModelSelector. A needs to know how to pass a ModelSelector through its interface, only only B derived from A and M derived from ModelSelector know specifically what type of thing needs to exist modeled.

    Any ideas on possible design improvements here?

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

  2. #2

    Re: Pure virtual template part?

    I'm going to give heave::any a attempt, run into if that is a reasonable arroyo.

  3. #3

    Re: Pure virtual template function?

    Pretty sure that virtual functions aren't immune to be templated (which makes sense when you think nigh information technology). Peradventure something like this might do what yous're looking for?

    Code:

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

  4. #4

    Re: Pure virtual template office?

    Hmm. That's certainly an interesting idea.....presumably, wherever the function chooseModel() got chosen, it would exist necessary to include the header containing the specialization, right? Otherwise you'd get undefined symbol errors? I guess I accept to decide whether or not that's acceptable.

  5. #5

    Re: Pure virtual template role?

    You know, information technology only crossed my mind... Why not merely directly specialize the chooseModel function? The whole nested struct thing in my first is just a compeltely unnecessary extra layer. If y'all exercise that so you should exist fine with simply having the specializations in one TU.

  6. #6

    Re: Pure virtual template function?

    Quote Originally Posted by Lindley View Post

    I tin't make ModelSelector a templated grade considering it needs to be passed through the generic framework in several places, and in that location's no reasonable manner they could know the particular instantiation.

    Pitiful, simply I don't get information technology. You'll be really exist passing an already instantiated blazon, won't you? Something similar ModelSelector<Model>. And then from the generic framework point of view it's just a type. I assume in this instance that the generic framework besides has templates.

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

    Code:

    template <grade T> class ModelSelector { /*...*/ };  template <course ModelSelector_Type> class DoNotKnowInstantiation {   //... };  typedef ModelSelector<ModelX> ModelSelectorType; DoNotKnowInstantiation<ModelSelectorType> genFramework1;  template <template <grade> class ModelSelector_Template> class 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.

    Yeah, templates and virtuals don't mix (even if they are not pure virtuals). One reason is that it would required as well much work from the compiler/linker considering information technology's not possible to know in accelerate how many instantiantions a particular virtual template member office (if information technology was possible) would exist. This imposes difficulties on amalgam the virtual tabular array.

  7. #seven

    Re: Pure virtual template function?

    Quote Originally Posted by ltcmelo View Post

    Sorry, only I don't get it. Yous'll be really be passing an already instantiated type, won't you? Something like ModelSelector<Model>. So from the generic framework indicate of view it's just a blazon. I assume in this example that the generic framework as well has templates.


    "Generic" may have been the incorrect discussion for the framework. It mostly leverages inheritance-based polymorphism for now. I'm non sure if that'southward the best design, but it'south not obvious how templates tin help here at a higher level.

  8. #viii

    Re: Pure virtual template office?

    Quote Originally Posted by Lindley View Post

    Then I idea perhaps if I made the cull() part itself templated, I could specialize it in the derived classes of ModelSelector. (It would simply ever be called from a derived grade of a different base, with a one-to-1 correspondence between the derived classes.)

    That doesn't make much sense to me. Why do you need to invoke a pure virtual office in a base form if the data you demand to pass to that function is dependent on the derived grade. In order words: why practise you need a virtual function anyway?

    Quote Originally Posted by Lindley View Post

    To summarize: I take base of operations classes A and ModelSelector. A needs to know how to pass a ModelSelector through its interface, merely only B derived from A and M derived from ModelSelector know specifically what type of affair needs to be modeled.

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

    Thank you, D Drmmr

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

    As long as human being ascribes to himself what is merely a posibility, he volition non work for the attainment of it. - P. D. Ouspensky


  9. #nine

    Re: Pure virtual template function?

    It'due south possible I *don't* need a virtual part. The design is all the same evolving. I haven't figure out entirely yet whether ModelSelector needs to be visible to the not-specific part of the code or not still.

  10. #10

    Re: Pure virtual template function?

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

    CT POlymorphism or Policy Based Design Or Traits.

    Thank you for your help.


Posting Permissions

  • You may not mail service new threads
  • You lot may not post replies
  • You may not post attachments
  • You lot may non edit your posts
  • BB code is On
  • Smilies are On
  • [IMG] code is On
  • [VIDEO] code is On
  • HTML code is Off

Click Here to Expand Forum to Full Width



Can Template Function Be Virtual,

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

Posted by: browningfroppres.blogspot.com

0 Response to "Can Template Function Be Virtual"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel