“template polymorphism” when calling function for templated parameter of base type, with derived type?

前端 未结 2 1554
抹茶落季
抹茶落季 2021-01-20 15:42

I\'ve got a template class:

template 
class TemplateClass
{
   //irrelevant what this class does
}

And two classes, one base

2条回答
  •  伪装坚强ぢ
    2021-01-20 16:07

    It is simple to solve this if you are not opposed to a more general situation:

    I would simply add a template parameter to your DoSomething function:

    template
    void DoSomething(const TemplateClass &tc)
    {
    }
    

    Of course, this doesn't allow you to directly constrain the parameter T to being a subclass of Base. But it will allow you to not write several overloads of DoSomething.

    Edit

    If your TemplateClass was intended to wrap a value of type T, then there may be a way to constrain to parameter for DoSomething.

    Suppose TemplateClass were declared something like

    template
    class TemplateClass
    {
    
    public:
    
        TemplateClass( T value )
        : Value(value)
        {
        }
    
        T Value;
    };
    

    Then DoSomething could be written as

    template
    void DoSomething( const TemplateClass &tc )
    {
        Base * b = tc.Value;
    }
    

    Now, during compilation, the line Base * b = tc.Value will only compile if T is set to a subclass of Base. Otherwise, it will fail due to an invalid conversion.

    Of course, this is a very simple example which may ignore some elements of good design. But the principle is there, and this is the only way I am currently aware of the produce such a constraint on a template parameter.

提交回复
热议问题