简体   繁体   English

为什么不能部分专门化类成员函数?

[英]Why can't you partially specialize a class member function?

Member functions of template classes can be fully specialized, eg 模板类的成员函数可以完全专门化,例如

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

would compile without problems. 会毫无问题地编译。 Note that foo() is not a template function so this is not about template function specialization (I can understand partial specialization is not allowed there as it would become incredibly confusing in combination with overloading). 请注意, foo()不是模板函数,所以这与模板函数的特殊化无关(我可以理解那里不允许局部特殊化,因为与重载结合会变得非常混乱)。 In my opinion, the above code is just a shorthand for the following template class specialization: 我认为,上面的代码只是以下模板专业化的简写:

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

Is this correct? 这个对吗?

In this case I'm wondering why a partial specialization with a similar shorthand is not allowed, ie why can I not write 在这种情况下,我想知道为什么不允许具有类似速记的部分专业化,即为什么我不能写

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

as a shorthand for 作为的简写

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

Since the 2nd fragment is legal and the 1st one would be completely equivalent to it (but without me having to explicitly copy all the other data members and maintain them in parallel forever), I don't see why the 1st one is not allowed. 由于第二个片段是合法的,而第一个片段则完全等同于它(但无需我必须显式复制所有其他数据成员并永远保持并行状态),我不明白为什么不允许第一个片段。

I am aware that this question has already been asked here and here , but I am not looking for an answer of the type "Indeed it is not allowed." 我知道这个问题已经在这里这里被问到 ,但是我不是在寻找“实际上是不允许的”答案。 or "It is not allowed because the standard says it isn't.", nor for ways to circumvent this problem. 或“不允许,因为标准说不是。”,也没有解决该问题的方法。 I am wondering why the standard does not allow it, ie is there a fundamental reason for this or could it be allowed in the future? 我想知道为什么该标准不允许这样做,也就是说,是否有根本原因,或者将来是否可以允许? I didn't find this in any of the apparent duplicate questions so far. 到目前为止,在所有明显的重复问题中我都没有找到这个。

The answer to your first question -- whether the second fragment is indeed equivalent to the first -- is "no". 第一个问题(第二个片段是否确实等于第一个片段)的答案是“否”。

In particular, your comment "// Copy all the other members from MyClass" doesn't really work: Those members have to remain members of a class template to ensure that they're only "instantiated on-demand". 特别是,您的注释“ //从MyClass复制所有其他成员”实际上并不起作用:这些成员必须保留为类模板的成员,以确保仅“按需实例化”。 Otherwise, you're likely to get spurious early errors on members you never actually used. 否则,您可能会在从未真正使用过的成员上得到虚假的早期错误。

(There is also an unfortunate issue that in C++ not all implicit instantiations can be written as an equivalent explicit specialization.) (还有一个不幸的问题,在C ++中,并非所有隐式实例都可以写为等效的显式专业化。)

That doesn't mean we couldn't come up with a specification to add similar functionality. 这并不意味着我们无法提出添加类似功能的规范。 It's just more subtle than "do the same as with full specialization", and so far I'm not aware of a serious effort to bring this to the standard. 它比“与完全专业化时的操作相同”要微妙得多,到目前为止,我还不知道要为此付出努力的认真努力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM