问题描述
我遇到了一个有趣的点,我不能解释或找到解释。考虑下面的模板定义(使用mingw g ++ 4.6.2编译):
template< typename T,typename S&
class Foo
{
public:
void f(){}
void g(){}
};如果我们想要,我们可以完全专门化任何单个成员函数:
模板<>
void Foo< char,int> :: f(){}
失败并出现无效使用不完整类型类Foo '错误:
模板< typename T ,类型名S>
void Foo< T,S *> :: f()
{
}
template< typename T>
void Foo< T,int> :: f()
{
}
我不知道为什么。这是一个有意识的设计决定,以避免一些我不能预见的问题吗?是否是监督?
提前感谢。
解决方案 部分专业化的概念仅存在于类模板(由§14.5.5描述)和成员模板(即本身是模板函数的模板类的成员,由§14.5.5.3/ 2描述)。它不存在于类模板的普通成员,也不存在于函数模板–因为它不是由标准描述的。
现在,你可能会认为通过给成员函数的部分特化的定义,如
模板< typename T>
void Foo< T,int> :: f()
{}
你隐含地定义类模板的部分特化: Foo gt;
。
后者意味着不可能通过简单的给出其成员之一的定义:该成员的存在不会遵循主模板的定义,因此定义它等同于定义 成员函数
另一方面,显式特化
的概念, / strong>(或者您称为全特化)存在于类模板的成员函数。它由标准明确描述:§14.7.3/ 14描述详细信息:
因此,对于成员的显式特化,类模板的其余部分的实例化隐含地–它是从主模板定义派生的,或者如果定义了任何部分专门化。
I came across an interesting point that I wasn't able to explain or find an explanation for. Consider the following template definition (compiled with mingw g++ 4.6.2):
template <typename T, typename S>
class Foo
{
public:
void f(){}
void g(){}
};
Should we want to, we can fully specialize any single member function:
template <>
void Foo<char,int>::f() {}
But partial specialization fails with an "invalid use of incomplete type 'class Foo<...>'" error:
template <typename T, typename S>
void Foo<T,S*>::f()
{
}
template <typename T>
void Foo<T,int>::f()
{
}
And I can't figure out why. Is it a conscious design decision made to avoid some problem I can't foresee? Is it an oversight?
Thanks in advance.
解决方案 The notion of partial specialization only exists for class templates (described by §14.5.5) and member templates (i.e. members of a template class that are themselves template functions, described by §14.5.5.3/2). It does not exist for ordinary members of class templates, nor does it exist for function templates – simply because it is not described by the Standard.
Now, you might argue that by giving the definition of a partial specialization of a member function, such as
template <typename T>
void Foo<T,int>::f()
{ }
you implicitly define a partial specialization of the class template: Foo<T,int>
. That, however, is explicitly ruled out by the Standard:
The latter implies that it is impossible to implicitly define a partial specialization by simply giving the definition of one of its members: The very existence of that member would not follow from the definition of the primary template, hence defining it is equivalent to defining a member function that wasn't declared, and that isn't allowed (even with non-template classes).
On the other hand, the notion of explicit specialization (or full specialization, as you call it) exists for member functions of class templates. It is explicitly described by the Standard:
§14.7.3/14 describes the details:
Hence, for explicit specializations of members, the instantiation of the rest of the class template works implicitly – it is derived from the primary template definition, or any partial specializations if defined.
这篇关于模板类不完全专门化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!