C++11: Template Function Specialization for Integer Types

后端 未结 5 1512
挽巷
挽巷 2020-12-04 19:52

Suppose I have a template function:

template
void f(T t)
{
    ...
}

and I want to write a specialization for all primiti

5条回答
  •  死守一世寂寞
    2020-12-04 20:27

    I would use overload resolution. That spares you from having to use the gross SFINAE hack. Unfortunately there are many areas where you can't avoid it, but this fortunately isn't one of those.

    template
    void f(T t)
    {
      f(t, std::is_integral());
    }
    
    template
    void f(T t, std::true_type)
    { 
      // ...
    }
    
    template
    void f(T t, std::false_type)
    { 
      // ...
    }
    

提交回复
热议问题