Template type check C++

前端 未结 6 744
日久生厌
日久生厌 2021-01-14 17:48

I have a template function which takes in objects. I need to determine whether the object is derived from a particular base class. If it is derived from the base class, I ne

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-14 18:31

    A true template specialization would be even better :)

    class A {
    public:
        char c;
    };
    
    template  void foo(const T & t)
    {
        std::cout << "this is generic.";
    }
    
    template <> void foo(const A & a)
    {
        std::cout << "this is specialized.";
    }
    
    int main(int argc, char * argv[])
    {
        foo(A());
    
        foo(int());
    }
    

提交回复
热议问题