How do I check my template class is of a specific classtype?

后端 未结 8 2640
小蘑菇
小蘑菇 2020-12-16 11:04

In my template-ized function, I\'m trying to check the type T is of a specific type. How would I do that?

p/s I knew the template specification way but I don\'t want

8条回答
  •  没有蜡笔的小新
    2020-12-16 11:41

    hmm because I had a large portion of same code until the 'specification' part.

    You can use overloading, but if a large part of the code would work for any type, you might consider extracting the differing part into a separate function and overload that.

    template 
    void specific(const T&);
    
    void specific(const std::string&);
    
    template 
    void something(const T& t)
    {
        //code that works on all types
        specific(t);
        //more code that works on all types
    }
    

提交回复
热议问题