c++ template specialization for all subclasses

后端 未结 5 584
隐瞒了意图╮
隐瞒了意图╮ 2020-12-30 09:27

I need to create a template function like this:

template
void foo(T a)
{
   if (T is a subclass of class Bar)
      do this
   else
      d         


        
5条回答
  •  时光说笑
    2020-12-30 10:05

    I like this clear style:

    void foo_detail(T a, const std::true_type&)
    {
        //do sub-class thing
    }
    
    void foo_detail(T a, const std::false_type&)
    {
        //do else
    }
    
    void foo(T a)
    {
        foo_detail(a, std::is_base_of::value);
    }
    

提交回复
热议问题