在这个网站上有以下段落:
在类主体外部定义显式专用类模板的成员时,不使用语法模板<>,除非它是显式专用成员类模板的成员,该模板专用于类模板,否则,语法将要求此类定义以嵌套模板所需的模板<参数>开头参数>参数。
我不知道突出显示的部分是什么意思。"否则"是指一般情况(不使用哪个模板<>(还是例外情况(必须使用哪个模板<>(?
我希望对该部分作出解释。
我们的模板:
template< typename T>
struct A {
struct B {}; // member class
template<class U> struct C { }; // member class template
};
请参阅以下代码:
template<> // specialization
struct A<int> {
void f(int); // member function of a specialization
};
// template<> not used for a member of a specialization
void A<int>::f(int) { /* ... */ }
在定义专用化的成员时,我们不使用 template<>
,因为这是一个普通的成员类。
但是,现在看到下一个代码:
template<> // specialization of a member class template
template<class U> struct A<char>::C {
void f();
};
// template<> is used when defining a member of an explicitly
// specialized member class template specialized as a class template
template<>
template<class U> void A<char>::C<U>::f() { /* ... */ }
在这里,我们专门化了已定义模板的成员模板。这就是为什么我们需要使用 template<>
来传递此成员模板所需的参数。在这种情况下,定义我们的成员模板需要class U
,因此为了传递,我们将需要关键字 template<>
.