chapter2 techniques 2.2 partial template specialization

template <class Window, class Controller>
class Widget
{
   ... generic implementation ...
};

template <>
class Widget<ModalDialog, MyController>
{
   ... specialized implementation ...
};

// Partial specialization of Widget
template <class Window>
class Widget<Window, MyController>
{
   ... partially specialized implementation ...
};

Unfortunately, partial template specialization does not apply to functions—be they member or
nonmember—which somewhat reduces the flexibility and the granularity of what you can do.

偏特化不能用于函数,全局的或是成员的。
•  Although you can totally specialize member functions of a class template, you cannot partially
specialize member functions.

你可以全特化一个成员函数,但是不能偏特化
•  You cannot partially specialize namespace-level (nonmember) template functions. The closest
thing to partial specialization for namespace-level template functions is overloading. For practical
purposes, this means that you have fine-grained specialization abilities only for the function
parameters—not for the return value or for internally used types. For example,
template <class T, class U> T Fun(U obj); // primary template
template <class U> void Fun<void, U>(U obj); // illegal partial
//        specialization
template <class T> T Fun (Window obj); // legal (overloading)

你不能偏特化一个全局的函数。但是可以重载全局函数。这说明只能控制函数参数,但是不能控制返回值和内部使用的类型。


This lack of granularity in partial specialization certainly makes life easier for compiler writers, but has
bad effects for developers. Some of the tools presented later (such as Int2Type and Type2Type)
specifically address this limitation of partial specialization.

可以通过Int2Type and Type2Type等弥补。

原文地址:https://www.cnblogs.com/cute/p/2142847.html