【发布时间】:2015-09-04 12:21:24
【问题描述】:
模板类的成员函数可以完全特化,例如
template<class A>
struct MyClass {
// Lots of other members
int foo();
};
template<class A>
MyClass<A>::foo() { return 42; }
template<>
MyClass<int>::foo() { return 0; }
可以毫无问题地编译。请注意,foo() 不是模板函数,因此这与 模板函数 特化无关(我可以理解部分特化是不允许的,因为它与重载结合起来会变得非常混乱)。在我看来,上面的代码只是以下模板 class 特化的简写:
template<class A>
struct MyClass {
// Lots of other members
int foo();
};
template<class A>
MyClass<A>::foo() { return 42; }
template<>
struct MyClass<int> {
// Copy all the other members from MyClass<A>
int foo();
};
template<>
MyClass<int>::foo() { return 0; }
这对吗?
在这种情况下,我想知道为什么不允许使用类似速记的 部分 特化,即为什么我不能写
template<class A, class B>
struct MyClass {
// Lots of other members
int foo();
};
template<class A, class B>
MyClass<A,B>::foo() { return 42; }
template<class B>
MyClass<int,B>::foo() { return 0; }
作为简写
template<class A, class B>
struct MyClass {
// Lots of other members
int foo();
};
template<class A, class B>
MyClass<A,B>::foo() { return 42; }
template<class B>
struct MyClass<int,B> {
// Copy all the other members from MyClass<A,B>
int foo();
};
template<class B>
MyClass<int,B>::foo() { return 0; }
由于第二个片段是合法的,而第一个片段完全等同于它(但我不必显式复制所有其他数据成员并永远并行维护它们),我不明白为什么第一个片段是不允许。
我知道here 和here 已经问过这个问题,但我不是在寻找“确实不允许”类型的答案。或“这是不允许的,因为标准说不允许。”,也没有解决这个问题的方法。我想知道为什么该标准不允许这样做,即这是否有根本原因,或者将来是否可以允许?到目前为止,我在任何明显的重复问题中都没有发现这一点。
【问题讨论】:
-
这个问题经常被问到。长答案在这里:gotw.ca/publications/mill17.htm
-
我可能错了,但我认为这篇文章是关于专门研究模板函数的。我的问题是关于专门化 template classes 的 non-template 成员函数,在我看来,这实际上是专门化类而不是函数(因为成员函数不是模板) .
-
那为什么允许完全专业化?
-
我认为,如果您认为它实际上是创建一个新的类专业化,就像我的问题中的前 2 个代码片段一样。
标签: c++ templates member-functions partial-specialization class-template