2

模板类的成员函数可以完全专门化,例如

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()不是模板函数,因此这与模板函数专业化无关(我可以理解部分专业化是不允许的,因为它与重载结合起来会变得非常混乱)。在我看来,上面的代码只是以下模板专业化的简写:

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; }

由于第二个片段是合法的,而第一个片段将完全等同于它(但我不必显式复制所有其他数据成员并永远并行维护它们),我不明白为什么不允许第一个片段。

我知道这个问题已经在这里这里被问过,但我不是在寻找“确实不允许”类型的答案。或“这是不允许的,因为标准说不允许。”,也没有解决这个问题的方法。我想知道为什么标准不允许这样做,即这是有根本原因还是将来会被允许?到目前为止,我在任何明显的重复问题中都没有发现这一点。

4

1 回答 1

5

您的第一个问题的答案 - 第二个片段是否确实等同于第一个 - 是“否”。

特别是,您的评论“// 复制 MyClass 中的所有其他成员”并没有真正起作用:这些成员必须保留类模板的成员,以确保它们只是“按需实例化”。否则,您可能会在从未实际使用过的成员上收到虚假的早期错误。

(还有一个不幸的问题是,在 C++ 中,并非所有隐式实例化都可以写成等效的显式特化。)

这并不意味着我们不能提出一个规范来添加类似的功能。它只是比“完全专业化时做同样的事情”更微妙,到目前为止,我还没有意识到将其带到标准的认真努力。

于 2015-09-04T18:51:06.600 回答