Can I use partial template specialization for a (non-member) function?

不问归期 提交于 2019-12-03 16:34:35

It is possible using class partitial specialization:

template<class A, class B>
struct Functor {
    static A convert(B source);
};

template<class B>
struct Functor<GrayScale, B> {
    static GrayScale convert(B source) {
         return Grayscale(IntensityFromRGB(source.r, source.g, source.b));
    }
};

// Common function
template<class A, class B>
A Convert(B source) {
   return typename Functor<A,B>::convert(source);
}

C++ does not allow partial specialization of function templates.

It doesn't even allow partial specialization of member function templates. When you define a function which is part of a partial specialization of a class template, that might look a bit like you've partially specialized the member function. But you haven't.

Herb Sutter discusses partial specialization

Partial specialization is a concept that only applies to classes, not free functions or member functions. What you probably want to do is just provide function (or member function) overloads, which sort of map to what partial and full specializations do for classes.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!