本文介绍了为什么允许嵌套类模板的部分特化,而不允许完全特化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    template<int x> struct A {
        template<int y> struct B {};.
        template<int y, int unused> struct C {};
    };

    template<int x> template<>
    struct A<x>::B<x> {}; // error: enclosing class templates are not explicitly specialized

    template<int x> template<int unused>
    struct A<x>::C<x, unused> {}; // ok

那么,如果外部类也不是专门化的,那么为什么不允许对内部嵌套类(或函数)进行显式专门化?奇怪的是,如果我只部分通过简单地添加一个虚拟模板参数来专门化内部类,我就可以解决这个问题.让事情变得更丑陋和更复杂,但它有效.

So why is the explicit specialization of a inner, nested class (or function) not allowed, if the outer class isn't specialized too? Strange enough, I can work around this behaviour if I only partially specialize the inner class with simply adding a dummy template parameter. Makes things uglier and more complex, but it works.

我会将完全特化视为部分特化的子集 - 特别是因为您可以通过添加虚拟参数将每个完全特化表示为部分特化.因此,对部分专业化和完全专业化之间的这种消歧对我来说并没有什么意义.

I would consider complete specializations as a subset of partial specializations - especially because you can express every complete specialization as a partial with adding a dummy parameter. So this disambiguation between partial and full specialization doesn't really make sense for me.

不幸的是,comp.std.c++ 中没有人敢回答,所以我悬赏再次将其放在这里.

Unfortunatly nobody at comp.std.c++ dared to answer, so I am putting it up here again with a bounty.

注意:对于一组外部类的内部类的递归模板,我需要此功能,并且内部参数的特化确实取决于外部模板参数.

Note: I need this feature for recursive templates of the inner class for a set of the outer class and the specialization of the inner parameter does depend on the outer template parameter.

推荐答案

我猜测为什么会发生这种情况:完整的专业化不再是模板类/函数",它们是真正的"类/方法,并且有真实的(链接器可见的)符号.但是对于部分专业模板中的完全专业模板,这将不是真的.可能做出这个决定只是为了简化编译器编写者的生活(并在此过程中让编码人员的生活更加艰难:P).

My guess as to why this happens: complete specializations are no longer "template classes/functions", they are are "real" classes/methods, and get to have real (linker-visible) symbols. But for a completely-specialized template inside a partially-specialized one, this would not be true.Probably this decision was taken just to simplify the life of compiler-writers (and make life harder for coders, in the process :P ).

这篇关于为什么允许嵌套类模板的部分特化,而不允许完全特化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 05:46