不能按要求这样做。 由于某种原因(我不肯定理由)explicit(即full)成员模板的专业化,只有在封闭式组别还具备专业资格时方可允许。 这一要求在语言标准中作了明确规定(见C++98、C++03和14.7.3/16在C++11中)。
At the same time, partial specializations of member class templates are allowed, which in many cases can be used as a workaround (albeit an ugly one). But, obviously, it is only applicable to member class templates. When it comes to member function templates, an alternative solution has to be used.
例如,一项可能的工作是将这一呼吁下放给一个模范班的固定成员,并将其专门化(常常建议将其作为一种比职能模板专业化更好的想法:。
template <class T>
class MyClass
{
template <int N, typename DUMMY = void> struct Func {
static void func() { printf("unspecialized
"); }
};
template <typename DUMMY> struct Func<0, DUMMY> {
static void func() { printf("specialized
"); }
};
template <int N> void func() { Func<N>::func(); }
};