Identifying primitive types in templates

后端 未结 8 844
南旧
南旧 2020-11-27 17:40

I am looking for a way to identify primitives types in a template class definition.

I mean, having this class :

template
class A{
void         


        
8条回答
  •  攒了一身酷
    2020-11-27 18:29

    Assuming by 'Primitive Type' you mean the built in types you can do a series of template specializations. Your code would become:

    template
    struct A{
        void doWork();
    private:
        T *t; 
    };
    
    template<> void A::doWork()
    {
        doSomething();
    }
    
    template<> void A::doWork()
    {
        doSomething();
    }
    
    // etc. for whatever types you like
    
    template void A::doWork()
    {
        doSomethingElse();
    }
    

提交回复
热议问题